線條調整外觀
此範例示範如何在地圖上調整線條 Feature 的外觀,包含以下功能:
- 取得指定線條 Feature:使用
map.data.getFeatureById()方法取得指定 id 的線條 Feature - 變更座標位置:使用
feature.setGeometry()方法變更線條的座標位置 - 變更線條顏色:使用
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>
<!-- Bootstrap CDN -->
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9"
crossorigin="anonymous"
/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css"
/>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm"
crossorigin="anonymous"
></script>
<style>
body { margin: 0; padding: 0; }
#map { width: 100vw; height: 100vh; }
#toolbar {
position: absolute;
top: 16px;
right: 16px;
width: fit-content;
height: fit-content;
}
</style>
</head>
<body>
<div id="map"></div>
<aside
id="toolbar"
class="d-flex flex-column align-items-center gap-3 bg-white p-2 pb-3 rounded-2 shadow"
>
<!-- 商標 -->
<div class="pb-2 border-bottom border-secondary-subtle">
<img
src="https://kw3dmap.autoking.com.tw/kingway-logo.png"
alt="Kingway map"
width="44"
height="44"
/>
</div>
<div>
<input
type="checkbox"
class="btn-check"
id="toggleBtnSetStyle"
autocomplete="off"
/>
<label
class="btn btn-outline-primary"
for="toggleBtnSetStyle"
data-bs-toggle="tooltip"
data-bs-placement="left"
data-bs-title="更改棕色線條位置及顏色"
>
<i class="bi bi-gear-fill"></i>
</label>
</div>
</aside>
<script type="module">
// Bootstrap 提示訊息 初始化
const tooltipList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
const tooltipInit = [...tooltipList].map(
(tooltip) => new bootstrap.Tooltip(tooltip)
);
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,
},
});
const toggleStyle = (checkboxElement) => {
// 取得 ID 為 3 的棕色線條
const brown_line = map.data.getFeatureById(3);
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 變更棕色線條座標位置及顏色:
- 可調整 setGeometry() 中的參數,變更線條的座標位置。
- 可調整 strokeColor,變更線條的顏色。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第一下按鈕
// 變更棕色線條的座標位置
brown_line.setGeometry([
[121.54893832410526, 25.051725290204974],
[121.54879558684992, 25.033221384937097],
]);
// 變更棕色線條的座標位置
// brown_line.setGeometry([
// [121.54893832410526, 25.061725290204974],
// [121.54879558684992, 25.043221384937097],
// ]);
map.data.setStyle((fea) => {
const { id } = fea.properties;
// 變更棕色線條的顏色為綠色
if (id === 3) return { strokeColor: "green" };
// 變更棕色線條的顏色為橘色
// if (id === 3) return { strokeColor: "orange" };
});
checkboxElement.removeAttribute("checked");
} else {
brown_line.setGeometry([
[121.544062, 25.051843],
[121.543635, 25.03336],
]);
map.data.setStyle((fea) => {
const { id } = fea.properties;
if (id === 3) return { strokeColor: "#DEB887" };
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnSetStyle = document.getElementById("toggleBtnSetStyle");
toggleBtnSetStyle.setAttribute("checked", true);
toggleBtnSetStyle.addEventListener("click", () => {
toggleStyle(toggleBtnSetStyle);
});
</script>
</body>
</html>
<script src="https://kw3dmap.localking.com.tw/openapi/loader/mapPlus-1.3.9.loader.js" crossorigin="anonymous" referrerpolicy="origin"></script>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm"
crossorigin="anonymous"
></script>
<script type="module">
// Bootstrap 提示訊息 初始化
const tooltipList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
const tooltipInit = [...tooltipList].map(
(tooltip) => new bootstrap.Tooltip(tooltip)
);
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,
},
});
const toggleStyle = (checkboxElement) => {
// 取得 ID 為 3 的棕色線條
const brown_line = map.data.getFeatureById(3);
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 變更棕色線條座標位置及顏色:
- 可調整 setGeometry() 中的參數,變更線條的座標位置。
- 可調整 strokeColor,變更線條的顏色。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第一下按鈕
// 變更棕色線條的座標位置
brown_line.setGeometry([
[121.54893832410526, 25.051725290204974],
[121.54879558684992, 25.033221384937097],
]);
// 變更棕色線條的座標位置
// brown_line.setGeometry([
// [121.54893832410526, 25.061725290204974],
// [121.54879558684992, 25.043221384937097],
// ]);
map.data.setStyle((fea) => {
const { id } = fea.properties;
// 變更棕色線條的顏色為綠色
if (id === 3) return { strokeColor: "green" };
// 變更棕色線條的顏色為橘色
// if (id === 3) return { strokeColor: "orange" };
});
checkboxElement.removeAttribute("checked");
} else {
brown_line.setGeometry([
[121.544062, 25.051843],
[121.543635, 25.03336],
]);
map.data.setStyle((fea) => {
const { id } = fea.properties;
if (id === 3) return { strokeColor: "#DEB887" };
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnSetStyle = document.getElementById("toggleBtnSetStyle");
toggleBtnSetStyle.setAttribute("checked", true);
toggleBtnSetStyle.addEventListener("click", () => {
toggleStyle(toggleBtnSetStyle);
});
</script>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9"
crossorigin="anonymous"
/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css"
/>
<style>
body { margin: 0; padding: 0; }
#map { width: 100vw; height: 100vh; }
#toolbar {
position: absolute;
top: 16px;
right: 16px;
width: fit-content;
height: fit-content;
}
</style>