多邊形的彈跳視窗
此範例示範如何在地圖上實現多邊形 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.Polygon([
[
{ lng: 121.51774279394641, lat: 25.02830346908604 },
{ lng: 121.51848131881536, lat: 25.027301911647328 },
{ lng: 121.51888179979937, lat: 25.027533732578846 },
{ lng: 121.51909424825834, lat: 25.02752555293422 },
{ lng: 121.51970762723715, lat: 25.027930516565775 },
{ lng: 121.51885490101677, lat: 25.028941308227516 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 0,
},
});
// 將左下多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.51995462852591, lat: 25.025617866336162 },
{ lng: 121.51980338007291, lat: 25.025138193693877 },
{ lng: 121.52038136523652, lat: 25.02489346203282 },
{ lng: 121.52039757042922, lat: 25.024815147797838 },
{ lng: 121.52070006733726, lat: 25.024731938867987 },
{ lng: 121.5207702898328, lat: 25.02494240840406 },
{ lng: 121.52064064830142, lat: 25.0249962493903 },
{ lng: 121.52074868291123, lat: 25.025422081810575 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 1,
},
});
// 將中間多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.5211730858556, lat: 25.02679842059477 },
{ lng: 121.52245995711473, lat: 25.025773483332074 },
{ lng: 121.52260219452722, lat: 25.026567885946108 },
{ lng: 121.52227143457367, lat: 25.027006377834994 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 2,
},
});
// 將右上多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.52376442749033, lat: 25.028484711165802 },
{ lng: 121.52366949650514, lat: 25.027805426509175 },
{ lng: 121.52506256692686, lat: 25.027523673142227 },
{ lng: 121.52514665561011, lat: 25.028383339796434 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 3,
},
});
// 將右下多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.52499224179655, lat: 25.026768269600595 },
{ lng: 121.52482990223632, lat: 25.025040181561437 },
{ lng: 121.52824880309294, lat: 25.02481642414638 },
{ lng: 121.52841649248722, lat: 25.026473113087235 },
],
]),
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.Polygon([
[
{ lng: 121.51774279394641, lat: 25.02830346908604 },
{ lng: 121.51848131881536, lat: 25.027301911647328 },
{ lng: 121.51888179979937, lat: 25.027533732578846 },
{ lng: 121.51909424825834, lat: 25.02752555293422 },
{ lng: 121.51970762723715, lat: 25.027930516565775 },
{ lng: 121.51885490101677, lat: 25.028941308227516 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 0,
},
});
// 將左下多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.51995462852591, lat: 25.025617866336162 },
{ lng: 121.51980338007291, lat: 25.025138193693877 },
{ lng: 121.52038136523652, lat: 25.02489346203282 },
{ lng: 121.52039757042922, lat: 25.024815147797838 },
{ lng: 121.52070006733726, lat: 25.024731938867987 },
{ lng: 121.5207702898328, lat: 25.02494240840406 },
{ lng: 121.52064064830142, lat: 25.0249962493903 },
{ lng: 121.52074868291123, lat: 25.025422081810575 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 1,
},
});
// 將中間多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.5211730858556, lat: 25.02679842059477 },
{ lng: 121.52245995711473, lat: 25.025773483332074 },
{ lng: 121.52260219452722, lat: 25.026567885946108 },
{ lng: 121.52227143457367, lat: 25.027006377834994 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 2,
},
});
// 將右上多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.52376442749033, lat: 25.028484711165802 },
{ lng: 121.52366949650514, lat: 25.027805426509175 },
{ lng: 121.52506256692686, lat: 25.027523673142227 },
{ lng: 121.52514665561011, lat: 25.028383339796434 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 3,
},
});
// 將右下多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.52499224179655, lat: 25.026768269600595 },
{ lng: 121.52482990223632, lat: 25.025040181561437 },
{ lng: 121.52824880309294, lat: 25.02481642414638 },
{ lng: 121.52841649248722, lat: 25.026473113087235 },
],
]),
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>