繪製多邊形

使用工具列繪製或刪除多邊形。端點座標顯示方式為 [lng, lat] (x, y)。

範例展示

原始碼

<!DOCTYPE html>
<html>
<head>
    <title>繪製線段並顯示端點座標 - Map8 Platform Documentation</title>
    <link rel="stylesheet" href="https://api.map8.zone/css/gomp.css?key=[YOUR_KEY_HERE]" />
    <link rel="stylesheet" href="https://api.map8.zone/css/gomp-draw.css?key=[YOUR_KEY_HERE]" />
    <style>
        #map{
            height: 400px;
            width: 100%;
        }
    </style>
</head>
<body>
    <div id="map"></div>
    <div id="coordindate"></div>

    <script type="text/javascript" src="https://api.map8.zone/maps/js/gomp.js?key=[YOUR_KEY_HERE]"></script>
    <script type="text/javascript" src="https://api.map8.zone/maps/js/gomp-draw.js?key=[YOUR_KEY_HERE]"></script>
    <script type="text/javascript">
        gomp.accessToken = '[YOUR_KEY_HERE]';
        var map = new gomp.Map({
            container: 'map', // 地圖容器 ID
            style: 'https://api.map8.zone/styles/go-life-maps-tw-style-std/style.json', // 地圖樣式檔案位置
            maxBounds: [[105, 15], [138.45858, 33.4]], // 台灣地圖區域
            center: [121.54885, 25.037853], // 初始中心座標,格式為 [lng, lat]
            zoom: 16, // 初始 ZOOM LEVEL; [0-20, 0 為最小 (遠), 20 ;最大 (近)]
            minZoom: 6, // 限制地圖可縮放之最小等級, 可省略, [0-19.99]
            maxZoom: 19.99, // 限制地圖可縮放之最大等級, 可省略 [0-19.99]
            localIdeographFontFamily: "'Microsoft YaHei', 'Microsoft JhengHei', 'MS Gothic', sans-serif", // 使用客戶端本機字體生成地圖文字,可加快載入速度
            speedLoad: false,
            attributionControl: false
        }).addControl(new gomp.AttributionControl({
            compact: false
        }));

        var draw = new gompDraw({
            displayControlsDefault: false,
            controls: {
            polygon: true,
            trash: true
            }
        });
        map.addControl(draw);

        var coordinateContainer = document.getElementById('coordindate');

        map.on('draw.create', updateArea);
        map.on('draw.delete', updateArea);
        map.on('draw.update', updateArea);

        function updateArea(e) {
            var data = draw.getAll();
            coordinateContainer.innerHTML = '';
            if (data.features.length > 0) {
                data.features.forEach(function(feature) {
                    feature.geometry.coordinates[0].forEach(function(coordinate) {
                        coordinateContainer.innerHTML += '[' + coordinate.join(', ') + '] (' + Math.round(map.project(coordinate).x) + ', ' + Math.round(map.project(coordinate).y) + ')<br />';
                    });
                });
            }
        }
    </script>
</body>
</html>