Skip to content

Generate embed TrackAsia map

Generate iframe TrackAsia map that can embedded on the website.

<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="utf-8" />
        <title>Generate embed TrackAsia map</title>
        <meta property="og:description" content="Generate iframe TrackAsia map that can embedded on the website." />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/[email protected]/dist/trackasia-gl.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/trackasia-gl.css" />
    <style>
        body { margin: 0; padding: 0; }
        html, body, #map { height: 100%; }
        #coordinates {
            background: rgba(0, 0, 0, 0.5);
            color: #fff;
            position: absolute;
            bottom: 10px;
            left: 10px;
            padding: 5px 10px;
            margin: 0;
            font-size: 11px;
            line-height: 16px;
            border-radius: 3px;
        }
    </style>
</head>
<body>

<div id="map"></div>
<div id="coordinates">
    <div id="iframeCode">
        <h2>Iframe code:</h2>
        <textarea id="iframeTextarea" rows="4" cols="30" readonly></textarea><br />

        <button onclick="generateIframeCode()">Generate</button>
        <button onclick="copyIframeCode()">Copy</button>
    </div>
</div>
<script>
    var map = new trackasiagl.Map({
        container: 'map',
        style: 'https://maps.track-asia.com/styles/v2/streets.json?key=public_key',
        center: {"lat":10.762622,"lng":106.660172},
        zoom: 6
    });

    var marker = new trackasiagl.Marker({ draggable: true })
        .setLngLat({"lat":10.762622,"lng":106.660172})
        .addTo(map);

    map.on('click', function (e) {
        marker.setLngLat(e.lngLat);
    });

    function generateIframeCode() {
        const zoom = map.getZoom();
        const lat = marker.getLngLat().lat;
        const lng = marker.getLngLat().lng;

        if (lat && lng && zoom) {
            const iframeCode = `<iframe width="70%" height="60%" style="border:none;" src="https://maps.track-asia.com/embed/?lat=${lat}&lng=${lng}&zoom=${zoom}"></iframe>`;
            document.getElementById('iframeTextarea').value = iframeCode;
        }
    }

    function copyIframeCode() {
        const iframeTextarea = document.getElementById('iframeTextarea');
        iframeTextarea.select();
        document.execCommand('copy');
        navigator.clipboard.writeText(iframeTextarea.value);
        alert('Copied successfully!');
    }
</script>

</body>
</html>