圓形圖層疊加

範例展示

原始碼

copy
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>圓形圖層疊加 - Map8 Platform Documentation</title>
    <link rel="stylesheet" href="https://api.map8.zone/css/gomp.css?key=[YOUR_KEY_HERE]" />
    <style>
        #map{
            height: 400px;
            width: 100%;
        }
    </style>
</head>
<body>
    <div id="map"></div>

    <script type="text/javascript" src="https://api.map8.zone/maps/js/gomp.js?key=[YOUR_KEY_HERE]"></script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/Turf.js/5.1.5/turf.min.js"></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]
            speedLoad: false,
            attributionControl: false
        }).addControl(new gomp.AttributionControl({
            compact: false
        }));

        map.on('load', function () {
            var center = map.getCenter();
            var radius = 200; // 單位為公尺
            var circle = turf.circle(turf.point([center.lng, center.lat]), radius / 1000, {});

            // 圓形
            map.addLayer({
                "id": "circle-fill",
                "type": "fill",
                "source": {
                    "type": "geojson",
                    "data": circle
                },
                "paint": {
                    "fill-color": '#4472EA',
                    "fill-opacity": 0.2
                }
            });

            // 圓形邊框
            map.addLayer({
                "id": "circle-outline",
                "type": "line",
                "source": {
                    "type": "geojson",
                    "data": circle
                },
                "paint": {
                    "line-color": '#4472EA',
                    "line-opacity": 0.3,
                    "line-width": 2,
                    "line-offset": 1
                }
            });

            // 半徑範圍說明文字
            map.addLayer({
                id: "circle-text",
                type: "symbol",
                source: {
                    type: "geojson",
                    data: {
                        type: "FeatureCollection",
                        features: [
                            {
                                type: "Feature",
                                geometry: {
                                    type: "Point",
                                    coordinates: [center.lng, center.lat]
                                }
                            }
                        ]
                    }
                },
                layout: {
                    'text-allow-overlap': true,
                    'text-field': '半徑 ' + radius + ' 公尺範圍',
                    'text-font': ["Open Sans Regular"],
                    'text-offset': [0, 0], // 位移 [X, Y] 單位 em
                    'text-size': 15
                },
                paint: {
                    'text-color': '#4472EA',
                    'text-halo-color': '#fff',
                    'text-halo-width': 2
                }
            });
        });
    </script>
</body>
</html>