Feature 的彈跳視窗
此範例示範如何在同一資料層中操作點、線、面與圓形,並於點擊時顯示彈跳視窗,包含以下功能:
- 新增點擊事件並建立彈跳視窗:使用
map.addListener("click")監聽點擊事件,再使用mapPlus.InfoWindow建立彈跳視窗,顯示 Feature 的屬性資訊,及使用mapPlus.Animation.GROW設定彈跳視窗的動畫效果
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Feature 的彈跳視窗</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.539489, lat: 25.043539 },
zoom: 14,
});
// 將藍色線條加入資料層
map.data.add({
// 接收內容包含經緯度的物件
geometry: new mapPlus.Data.LineString([
{ lng: 121.519633, lat: 25.045716 },
{ lng: 121.535425, lat: 25.041954 },
{ lng: 121.543594, lat: 25.041654 },
{ lng: 121.557682, lat: 25.041332 },
]),
properties: {
strokeWeight: 15,
strokeColor: "#20D0E4",
id: 1,
},
});
// 將黃色線條加入資料層
map.data.add({
geometry: new mapPlus.Data.LineString([
{ lng: 121.533018, lat: 25.051893 },
{ lng: 121.532708, lat: 25.033877 },
]),
properties: {
strokeWeight: 15,
strokeColor: "#F0E68C",
id: 2,
},
});
// 將棕色線條加入資料層
map.data.add({
geometry: new mapPlus.Data.LineString([
{ lng: 121.544062, lat: 25.051843 },
{ lng: 121.543635, lat: 25.03336 },
]),
properties: {
strokeWeight: 15,
strokeColor: "#DEB887",
id: 3,
},
});
// 將左上的座標點加入資料層
map.data.add({
geometry: new mapPlus.Data.Point({
lng: 121.52344273535077,
lat: 25.047738045570924,
}),
properties: {
id: 4,
},
});
// 將中上的綠色多邊形加入資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.5366316419258, lat: 25.04843241829805 },
{ lng: 121.53648492590236, lat: 25.04493869783461 },
{ lng: 121.54219657216049, lat: 25.04469051801719 },
{ lng: 121.54090283425433, lat: 25.048422667727493 },
],
]),
properties: {
fillColor: "PaleGreen",
strokeWeight: 10,
strokeColor: "LimeGreen",
id: 5,
},
});
// 將右上的座標點加入資料層
map.data.add({
geometry: new mapPlus.Data.Point({
lng: 121.55305046036266,
lat: 25.045764018256193,
}),
properties: { id: 6 },
});
// 將左下的綠色多邊形加入資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.52225419281643, lat: 25.041035044764342 },
{ lng: 121.52110552016177, lat: 25.037042101709034 },
{ lng: 121.52727354398672, lat: 25.034082972950685 },
{ lng: 121.5287424696262, lat: 25.039473741237558 },
],
]),
properties: {
fillColor: "PaleGreen",
strokeWeight: 10,
strokeColor: "LimeGreen",
id: 7,
},
});
// 將中下的座標點加入資料層
map.data.add({
geometry: new mapPlus.Data.Point({
lng: 121.53554486242585,
lat: 25.036656919945415,
}),
properties: { id: 8 },
});
// 將右下的綠色多邊形加入資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.54881096991483, lat: 25.0378725451842 },
{ lng: 121.54871579901152, lat: 25.033312945049317 },
{ lng: 121.55346639302968, lat: 25.03321918583258 },
{ lng: 121.55342126831317, lat: 25.03460635234474 },
{ lng: 121.5544914913757, lat: 25.035501753482535 },
{ lng: 121.55449750188853, lat: 25.037701856883217 },
],
]),
properties: {
fillColor: "PaleGreen",
strokeWeight: 10,
strokeColor: "LimeGreen",
id: 9,
},
});
// 將左上的黃色圓形加入資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.52744273535077,
lat: 25.048738045570925,
radius: 200,
}),
properties: {
strokeWeight: 10,
fillColor: "#F1E68C",
strokeColor: "#ffcf0f",
fillOpacity: 1,
id: 10,
},
});
// 將右上的黃色圓形加入資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.54795046036266,
lat: 25.046864018256194,
radius: 300,
}),
properties: {
strokeWeight: 10,
fillColor: "#F1E68C",
strokeColor: "#ffcf0f",
fillOpacity: 1,
id: 11,
},
});
// 將中下的黃色圓形加入資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.53954486242586,
lat: 25.037656919945416,
radius: 120,
}),
properties: {
strokeWeight: 10,
fillColor: "#F1E68C",
strokeColor: "#ffcf0f",
fillOpacity: 1,
id: 12,
},
});
/* ══════════════════════════════════════════════════════════════════════════
* 新增點擊事件並建立彈跳視窗:
- 調整 content 可設置訊息視窗的內容顯示,目前是顯示 feature 的 id。
══════════════════════════════════════════════════════════════════════════ */
map.addListener("click", (feature) => {
// 取得 feature 的 id 及座標
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.539489, lat: 25.043539 },
zoom: 14,
});
// 將藍色線條加入資料層
map.data.add({
// 接收內容包含經緯度的物件
geometry: new mapPlus.Data.LineString([
{ lng: 121.519633, lat: 25.045716 },
{ lng: 121.535425, lat: 25.041954 },
{ lng: 121.543594, lat: 25.041654 },
{ lng: 121.557682, lat: 25.041332 },
]),
properties: {
strokeWeight: 15,
strokeColor: "#20D0E4",
id: 1,
},
});
// 將黃色線條加入資料層
map.data.add({
geometry: new mapPlus.Data.LineString([
{ lng: 121.533018, lat: 25.051893 },
{ lng: 121.532708, lat: 25.033877 },
]),
properties: {
strokeWeight: 15,
strokeColor: "#F0E68C",
id: 2,
},
});
// 將棕色線條加入資料層
map.data.add({
geometry: new mapPlus.Data.LineString([
{ lng: 121.544062, lat: 25.051843 },
{ lng: 121.543635, lat: 25.03336 },
]),
properties: {
strokeWeight: 15,
strokeColor: "#DEB887",
id: 3,
},
});
// 將左上的座標點加入資料層
map.data.add({
geometry: new mapPlus.Data.Point({
lng: 121.52344273535077,
lat: 25.047738045570924,
}),
properties: {
id: 4,
},
});
// 將中上的綠色多邊形加入資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.5366316419258, lat: 25.04843241829805 },
{ lng: 121.53648492590236, lat: 25.04493869783461 },
{ lng: 121.54219657216049, lat: 25.04469051801719 },
{ lng: 121.54090283425433, lat: 25.048422667727493 },
],
]),
properties: {
fillColor: "PaleGreen",
strokeWeight: 10,
strokeColor: "LimeGreen",
id: 5,
},
});
// 將右上的座標點加入資料層
map.data.add({
geometry: new mapPlus.Data.Point({
lng: 121.55305046036266,
lat: 25.045764018256193,
}),
properties: { id: 6 },
});
// 將左下的綠色多邊形加入資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.52225419281643, lat: 25.041035044764342 },
{ lng: 121.52110552016177, lat: 25.037042101709034 },
{ lng: 121.52727354398672, lat: 25.034082972950685 },
{ lng: 121.5287424696262, lat: 25.039473741237558 },
],
]),
properties: {
fillColor: "PaleGreen",
strokeWeight: 10,
strokeColor: "LimeGreen",
id: 7,
},
});
// 將中下的座標點加入資料層
map.data.add({
geometry: new mapPlus.Data.Point({
lng: 121.53554486242585,
lat: 25.036656919945415,
}),
properties: { id: 8 },
});
// 將右下的綠色多邊形加入資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.54881096991483, lat: 25.0378725451842 },
{ lng: 121.54871579901152, lat: 25.033312945049317 },
{ lng: 121.55346639302968, lat: 25.03321918583258 },
{ lng: 121.55342126831317, lat: 25.03460635234474 },
{ lng: 121.5544914913757, lat: 25.035501753482535 },
{ lng: 121.55449750188853, lat: 25.037701856883217 },
],
]),
properties: {
fillColor: "PaleGreen",
strokeWeight: 10,
strokeColor: "LimeGreen",
id: 9,
},
});
// 將左上的黃色圓形加入資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.52744273535077,
lat: 25.048738045570925,
radius: 200,
}),
properties: {
strokeWeight: 10,
fillColor: "#F1E68C",
strokeColor: "#ffcf0f",
fillOpacity: 1,
id: 10,
},
});
// 將右上的黃色圓形加入資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.54795046036266,
lat: 25.046864018256194,
radius: 300,
}),
properties: {
strokeWeight: 10,
fillColor: "#F1E68C",
strokeColor: "#ffcf0f",
fillOpacity: 1,
id: 11,
},
});
// 將中下的黃色圓形加入資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.53954486242586,
lat: 25.037656919945416,
radius: 120,
}),
properties: {
strokeWeight: 10,
fillColor: "#F1E68C",
strokeColor: "#ffcf0f",
fillOpacity: 1,
id: 12,
},
});
/* ══════════════════════════════════════════════════════════════════════════
* 新增點擊事件並建立彈跳視窗:
- 調整 content 可設置訊息視窗的內容顯示,目前是顯示 feature 的 id。
══════════════════════════════════════════════════════════════════════════ */
map.addListener("click", (feature) => {
// 取得 feature 的 id 及座標
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>