建立群聚標示
範例展示
原始碼
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">
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.22649, 24.95997], // 初始中心座標,格式為 [lng, lat]
zoom: 12.92, // 初始 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
}));
var imageEl = document.createElement('img');
// 請代換為您的 Base64 圖檔
imageEl.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAqCAYAAACgLjskAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAjlJREFUeNrUl8FOwkAQhocVjAYJmBgveCDxBTTqwZN9A/EN8Amsid7x7EWfQHgC9A3grInwACZcuKiJEkI0ouBOnZJa2t2Bbk38k2VLd7ZfZ3d2Z5sYjUag0vbpc05WJVksKlmfSVeWOpXK3fnKq+p5iTAggcqyHMF0usR+YeBAoIQV8W0DvOEKvS5J6LW/QQTAbFnVIsCA+tbks0pKD8ngCszqUHpamQDSMNYgHh24w+sAKUDaEYdRN6cFDCR3Du0YYe6c2o6HWydPLO+Ggzd4f3mAz/4jjIaDn/kQKUimV2FheR1EapHlZVL+FHWwr48e9Du3Y5Ar/D/odZyXSOd3YG4+o/OyKAio9CwI5gejDdpq5AAtlQUOowrmhaKtRpbQDScOF1cM26zgvDlXHFuhmz/TUgIx1DH0ueLYaocU1xlXHFsENlQGuKg5b442aKtRQ1CmVg4rLmoVFNvQhrHbXAtKtErhDrK0tgupTP4XGK/xHrZpdpkx0M0W6OUexKuGzBaWN1vELXscpZLclNVZjLBjYkwcMTAr7xuG3UhYMWwd4pmmZRDWomcGL3w6S1qGoPgMy38+ndhpDEEDYaFbW0RoKEy5l84IVcK0m/eUUC1M+THjO/5vyOpeY7bprrVI6cmzMVRVX0wcGBtIKivaLoxkfJ+X7RAvq9QGpj100gvzXrSg8QWQt0NXepczdogKy2ue67rRU1uIvJDmXwCb/wo4ddBQ4LRpqRSm7ZucMf3UZ+wH3wIMAPnJ+VlcHpUWAAAAAElFTkSuQmCC';
map.on('load', function () {
map.addSource("housesale", {
type: "geojson",
data: "https://www.map8.zone/js/vector/housesale.json", // 此範例所使用的檔案請見 https://www.map8.zone/js/vector/housesale.json
cluster: true,
clusterMaxZoom: 14, // 顯示群聚點的最大 zoom level
clusterRadius: 58 // 設定群聚半徑 (單位公尺 / 預設 58)
});
map.addLayer({
id: 'clusters_outline',
type: 'circle',
source: 'housesale',
filter: ['has', 'point_count'],
paint: {
"circle-color": [
"step",
["get", "point_count"],
"rgba(0,0,0,.6)",
10,
"rgba(0,0,0,.6)",
50,
"rgba(0,0,0,.6)"
],
'circle-radius': [
'step',
['get', 'point_count'],
21.2,
31.2,
41.2,
51.2,
61.2
]
}
});
map.addLayer({
id: 'clusters',
type: 'circle',
source: 'housesale',
filter: ['has', 'point_count'],
paint: {
"circle-color": [
"step",
["get", "point_count"],
// 數量小於 10
"#d8f5a2",
10,
"#8ce99a",
20,
"#40c057",
30,
"#40c057",
40,
"#37b24d",
50,
"#37b24d",
60,
"#2f9e44",
70,
"#2b8a3e",
80,
"#74b816",
90,
"#66a80f",
100,
"#5c940d"
],
'circle-radius': [
'step',
['get', 'point_count'],
20,
30,
40,
50,
60
]
}
});
map.addLayer({
id: "cluster-count",
type: "symbol",
source: "housesale",
filter: ["has", "point_count"],
layout: {
"text-field": "{point_count}",
"text-size": 16,
"text-font": ["Noto Sans Regular"],
}
});
map.addImage("image", imageEl);
map.addLayer({
id: "unclustered-point",
type: "symbol",
source: "housesale",
filter: ["!", ["has", "point_count"]],
layout: {
"icon-image": "image",
"icon-size": 0.7
},
});
map.on('click', 'clusters', function (e) {
var features = map.queryRenderedFeatures(e.point, { layers: ['clusters'] });
var clusterId = features[0].properties.cluster_id;
map.getSource('housesale').getClusterExpansionZoom(clusterId, function (err, zoom) {
if (err)
return;
map.easeTo({
center: features[0].geometry.coordinates,
zoom: zoom
});
});
});
map.on('mouseenter', 'clusters', function () {
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseleave', 'clusters', function () {
map.getCanvas().style.cursor = '';
});
});
</script>
</body>
</html>