座標點的彈跳視窗
此範例示範如何在地圖上與座標點 Feature 進行互動並建立彈跳視窗,包含以下功能:
- 點擊並建立彈跳視窗:使用
map.addListener("click", callback)監聽點擊事件,並使用mapPlus.InfoWindow建立彈跳視窗,顯示 Feature 的屬性資訊
<!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.52424082, lat: 25.0265365823 },
zoom: 15.8,
});
// 將座標點加入資料層
map.data.add({
// 接收內容包含經緯度的物件
geometry: new mapPlus.Data.Point({
lng: 121.52055832858763,
lat: 25.025126634176872,
}),
properties: {
textField: "Size12",
textSize: 12,
id: 1,
},
});
map.data.add({
geometry: new mapPlus.Data.Point({
lng: 121.52225776991352,
lat: 25.026686419244356,
}),
properties: {
textField: "Size15",
textSize: 15,
id: 2,
},
});
map.data.add({
geometry: new mapPlus.Data.Point({
lng: 121.5260511577948,
lat: 25.025200947748637,
}),
properties: {
textField: "Size20",
textSize: 20,
id: 3,
},
});
/* ══════════════════════════════════════════════════════════════════════════
* 點擊並建立彈跳視窗:
- 可調整 content 來變更視窗文字內容。
══════════════════════════════════════════════════════════════════════════ */
map.addListener("click", (feature) => {
const {
geometry: { lngLat },
properties: { id },
} = feature;
// 顯示 ID 的訊息視窗
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.52424082, lat: 25.0265365823 },
zoom: 15.8,
});
// 將座標點加入資料層
map.data.add({
// 接收內容包含經緯度的物件
geometry: new mapPlus.Data.Point({
lng: 121.52055832858763,
lat: 25.025126634176872,
}),
properties: {
textField: "Size12",
textSize: 12,
id: 1,
},
});
map.data.add({
geometry: new mapPlus.Data.Point({
lng: 121.52225776991352,
lat: 25.026686419244356,
}),
properties: {
textField: "Size15",
textSize: 15,
id: 2,
},
});
map.data.add({
geometry: new mapPlus.Data.Point({
lng: 121.5260511577948,
lat: 25.025200947748637,
}),
properties: {
textField: "Size20",
textSize: 20,
id: 3,
},
});
/* ══════════════════════════════════════════════════════════════════════════
* 點擊並建立彈跳視窗:
- 可調整 content 來變更視窗文字內容。
══════════════════════════════════════════════════════════════════════════ */
map.addListener("click", (feature) => {
const {
geometry: { lngLat },
properties: { id },
} = feature;
// 顯示 ID 的訊息視窗
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>