Feature 的互動效果
此範例示範如何在同一資料層中操作點、線、面與圓形,並透過滑鼠事件覆蓋幾何圖形樣式,包含以下功能:
- 覆蓋樣式:使用
map.addListener("mouseover")監聽滑鼠移入事件,並使用map.data.overrideStyle()即時覆蓋線寬、外框顏色與點 icon - 還原樣式:使用
map.addListener("mouseout")監聽滑鼠移出事件,並使用map.data.revertStyle()還原原始樣式
info
詳細使用說明請參考「覆蓋預設樣式」。
<!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,
},
});
/* ══════════════════════════════════════════════════════════════════════════
* 覆蓋樣式:
- 調整 strokeWeight 可覆蓋原始的線條/多邊形/圓形外框寬度。
- 調整 strokeColor 可覆蓋原始的線條/多邊形/圓形外框顏色。
- 調整 icon 可覆蓋原始的座標點 icon。
══════════════════════════════════════════════════════════════════════════ */
map.addListener("mouseover", (feature) => {
map.data.revertStyle();
map.data.overrideStyle(feature, {
// 線條寬度
strokeWeight: 18,
// 線條顏色
strokeColor: "darkslategray",
// 自訂 icon
icon: "B060112",
});
});
// 還原原始樣式
map.addListener("mouseout", () => {
map.data.revertStyle();
});
</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,
},
});
/* ══════════════════════════════════════════════════════════════════════════
* 覆蓋樣式:
- 調整 strokeWeight 可覆蓋原始的線條/多邊形/圓形外框寬度。
- 調整 strokeColor 可覆蓋原始的線條/多邊形/圓形外框顏色。
- 調整 icon 可覆蓋原始的座標點 icon。
══════════════════════════════════════════════════════════════════════════ */
map.addListener("mouseover", (feature) => {
map.data.revertStyle();
map.data.overrideStyle(feature, {
// 線條寬度
strokeWeight: 18,
// 線條顏色
strokeColor: "darkslategray",
// 自訂 icon
icon: "B060112",
});
});
// 還原原始樣式
map.addListener("mouseout", () => {
map.data.revertStyle();
});
</script>
<style>
body { margin: 0; padding: 0; }
#map { width: 100vw; height: 100vh; }
</style>