線條的點擊事件
此範例示範如何在地圖上實現線條 Feature 的點擊事件,包含以下功能:
- 更改游標樣式:使用
map.addListener()監聽mouseover和mouseout事件,在可點擊圖形範圍內更改游標圖案 - 點擊線條變色:使用
map.addListener("click")監聽點擊事件,變更線條顏色 - 設定不可點擊:使用
map.data.setStyle()設定特定線條為不可點擊
info
詳細使用說明請參考「資料層」。
<!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.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: 20,
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: 20,
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: 20,
strokeColor: "#DEB887",
id: 3,
},
});
/* ══════════════════════════════════════════════════════════════════════════
* 更改游標樣式
══════════════════════════════════════════════════════════════════════════ */
map.addListener("mouseover", (feature) => {
map.data.revertStyle();
// 滑鼠游標在可點擊幾何圖案範圍內更改游標圖案
map.data.setStyle({ cursor: "pointer" });
});
map.addListener("mouseout", (event) => {
map.data.revertStyle();
map.data.setStyle({ cursor: "" });
});
/* ══════════════════════════════════════════════════════════════════════════
* 點擊線條變色:
- 可調整 strokeColor 的值,更換為其他線條顏色。
══════════════════════════════════════════════════════════════════════════ */
map.addListener("click", (feature) => {
map.data.setStyle((f) => {
if (f.getProperty("id") !== feature.getProperty("id")) return;
// 點擊線條會變成金色,再次點擊則變為灰色
const color = f.getProperty("strokeColor") === "gold" ? "gray" : "gold";
// 點擊線條會變成橘色,再次點擊則變為白色
// const color = f.getProperty("strokeColor") === "orange" ? "white" : "orange";
return { strokeColor: color };
});
});
/* ══════════════════════════════════════════════════════════════════════════
* 設定不可點擊:
- 可更換其他條件來設置其他線條為不可點擊。
══════════════════════════════════════════════════════════════════════════ */
map.data.setStyle((f) => {
// 將 ID 為 1 的藍色線條設定為不可點擊
if (f.getProperty("id") === 1) return { clickable: false };
// 將 ID 為 2 的黃色線條設定為不可點擊
// if (f.getProperty("id") === 2) return { clickable: false };
});
</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: 20,
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: 20,
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: 20,
strokeColor: "#DEB887",
id: 3,
},
});
/* ══════════════════════════════════════════════════════════════════════════
* 更改游標樣式
══════════════════════════════════════════════════════════════════════════ */
map.addListener("mouseover", (feature) => {
map.data.revertStyle();
// 滑鼠游標在可點擊幾何圖案範圍內更改游標圖案
map.data.setStyle({ cursor: "pointer" });
});
map.addListener("mouseout", (event) => {
map.data.revertStyle();
map.data.setStyle({ cursor: "" });
});
/* ══════════════════════════════════════════════════════════════════════════
* 點擊線條變色:
- 可調整 strokeColor 的值,更換為其他線條顏色。
══════════════════════════════════════════════════════════════════════════ */
map.addListener("click", (feature) => {
map.data.setStyle((f) => {
if (f.getProperty("id") !== feature.getProperty("id")) return;
// 點擊線條會變成金色,再次點擊則變為灰色
const color = f.getProperty("strokeColor") === "gold" ? "gray" : "gold";
// 點擊線條會變成橘色,再次點擊則變為白色
// const color = f.getProperty("strokeColor") === "orange" ? "white" : "orange";
return { strokeColor: color };
});
});
/* ══════════════════════════════════════════════════════════════════════════
* 設定不可點擊:
- 可更換其他條件來設置其他線條為不可點擊。
══════════════════════════════════════════════════════════════════════════ */
map.data.setStyle((f) => {
// 將 ID 為 1 的藍色線條設定為不可點擊
if (f.getProperty("id") === 1) return { clickable: false };
// 將 ID 為 2 的黃色線條設定為不可點擊
// if (f.getProperty("id") === 2) return { clickable: false };
});
</script>
<style>
body { margin: 0; padding: 0; }
#map { width: 100vw; height: 100vh; }
</style>