圓形的彈跳視窗
此範例示範如何在地圖上實現圓形 Feature 的彈跳視窗,包含以下功能:
- 點擊並建立彈跳視窗:使用
map.addListener("click")監聽點擊事件,再使用mapPlus.InfoWindow建立彈跳視窗,顯示 Feature 的屬性資訊,及使用mapPlus.Animation.GROW設定彈跳視窗的動畫效果
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>圓形的彈跳視窗</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://kw3dmap.localking.com.tw/openapi/loader/mapPlus-1.3.9.loader.js" crossorigin="anonymous" referrerpolicy="origin"></script>
<style>
body { margin: 0; padding: 0; }
#map { width: 100vw; height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<script type="module">
const map = await new mapPlus(document.getElementById("map"), {
accessKey: 'get_your_key', // 聯絡勤崴國際,取得專屬key
accessToken: 'get_your_token', // 聯絡勤崴國際,取得專屬token
style: 'https://kw3dmap.localking.com.tw/openapi/map/kwmap.etxt', // 樣式
center: { lng: 121.5227224646543, lat: 25.02701676669362 },
zoom: 15.8,
});
// 將左上圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.518793,
lat: 25.027923,
radius: 80,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 0,
},
});
// 將左下圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.520326,
lat: 25.025145,
radius: 70,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 1,
},
});
// 將中間圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.522127,
lat: 25.026536,
radius: 60,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 2,
},
});
// 將右上圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.524411,
lat: 25.02805,
radius: 75,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 3,
},
});
// 將右下圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.526622,
lat: 25.025525,
radius: 120,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 4,
},
});
/* ══════════════════════════════════════════════════════════════════════════
* 點擊並建立彈跳視窗:
- 可調整 content 來變更視窗文字內容。
══════════════════════════════════════════════════════════════════════════ */
map.addListener("click", (feature) => {
const {
geometry: { lngLat },
properties: { id },
} = feature;
new mapPlus.InfoWindow({
position: lngLat,
content: "ID:" + id,
animation: mapPlus.Animation.GROW,
}).open();
});
</script>
</body>
</html>
<script src="https://kw3dmap.localking.com.tw/openapi/loader/mapPlus-1.3.9.loader.js" crossorigin="anonymous" referrerpolicy="origin"></script>
<script type="module">
const map = await new mapPlus(document.getElementById("map"), {
accessKey: 'get_your_key', // 聯絡勤崴國際,取得專屬key
accessToken: 'get_your_token', // 聯絡勤崴國際,取得專屬token
style: 'https://kw3dmap.localking.com.tw/openapi/map/kwmap.etxt', // 樣式
center: { lng: 121.5227224646543, lat: 25.02701676669362 },
zoom: 15.8,
});
// 將左上圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.518793,
lat: 25.027923,
radius: 80,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 0,
},
});
// 將左下圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.520326,
lat: 25.025145,
radius: 70,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 1,
},
});
// 將中間圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.522127,
lat: 25.026536,
radius: 60,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 2,
},
});
// 將右上圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.524411,
lat: 25.02805,
radius: 75,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 3,
},
});
// 將右下圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.526622,
lat: 25.025525,
radius: 120,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 4,
},
});
/* ══════════════════════════════════════════════════════════════════════════
* 點擊並建立彈跳視窗:
- 可調整 content 來變更視窗文字內容。
══════════════════════════════════════════════════════════════════════════ */
map.addListener("click", (feature) => {
const {
geometry: { lngLat },
properties: { id },
} = feature;
new mapPlus.InfoWindow({
position: lngLat,
content: "ID:" + id,
animation: mapPlus.Animation.GROW,
}).open();
});
</script>
<style>
body { margin: 0; padding: 0; }
#map { width: 100vw; height: 100vh; }
</style>