線條的顯示狀態
此範例示範如何在地圖上控制整個線條圖層的可視性,包含以下功能:
- 圖層隱藏/顯示:使用
map.data.setStyle()方法設定lineVisible: false/true隱藏/顯示所有線條 - GeoJSON 查看:使用
map.data.toGeoJson()方法查看整個資料層的 GeoJSON 格式內容
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;
}
#toGeoJson {
position: absolute;
top: 0;
left: 0;
bottom: 0;
width: 50%;
height: 100%;
overflow: auto;
background: rgba(255, 255, 255, 0.8);
display: none;
}
</style>
</head>
<body>
<div id="map"></div>
<pre id="toGeoJson"></pre>
<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>
<div>
<input
type="checkbox"
class="btn-check"
id="toggleBtnToGeoJson"
autocomplete="off"
/>
<label
class="btn btn-outline-primary"
for="toggleBtnToGeoJson"
data-bs-toggle="tooltip"
data-bs-placement="left"
data-bs-title="查看GeoJSON"
><i class="bi bi-file-text-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: [121.539489, 25.043539],
pitch: 0,
bearing: 0,
zoom: 14,
maxZoom: 20,
minZoom: 7,
});
// 將藍色線條加入資料層
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: 25,
strokeColor: "#20D0E4",
id: 1,
name: "Bannan Line",
},
});
// 將黃色線條加入資料層
map.data.add({
geometry: new mapPlus.Data.LineString([
{ lng: 121.533018, lat: 25.051893 },
{ lng: 121.532708, lat: 25.033877 },
]),
properties: {
strokeWeight: 25,
strokeColor: "#F0E68C",
id: 1,
name: "Zhonghe-Xinlu Line",
},
});
// 將棕色線條加入資料層
map.data.add({
geometry: new mapPlus.Data.LineString([
{ lng: 121.544062, lat: 25.051843 },
{ lng: 121.543635, lat: 25.03336 },
]),
properties: {
strokeWeight: 25,
strokeColor: "#DEB887",
id: 2,
name: "Wenhu Line",
},
});
/* ══════════════════════════════════════════════════════════════════════════
* 線條圖層隱藏/顯示
══════════════════════════════════════════════════════════════════════════ */
const toggleStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
// 點擊第一個按鈕第一下,隱藏所有線條
map.data.setStyle({ lineVisible: false });
checkboxElement.removeAttribute("checked");
} else {
// 點擊第一個按鈕第二下,顯示所有線條
map.data.setStyle({ lineVisible: true });
checkboxElement.setAttribute("checked", true);
}
};
// 可移除下方註解,以隱藏線條圖層
// map.data.setStyle({ lineVisible: false });
const toggleBtnSetStyle = document.getElementById("toggleBtnSetStyle");
toggleBtnSetStyle.setAttribute("checked", true);
toggleBtnSetStyle.addEventListener("click", () => {
toggleStyle(toggleBtnSetStyle);
});
const toggleToGeoJson = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
// 取得資料層 Geojson 的資料,並顯示內容
map.data.toGeoJson((data) => {
const element = document.getElementById("toGeoJson");
element.style.display = "block";
element.innerHTML = JSON.stringify(data, null, 2);
});
checkboxElement.removeAttribute("checked");
} else {
document.getElementById("toGeoJson").style.display = "none";
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnToGeoJson = document.getElementById("toggleBtnToGeoJson");
toggleBtnToGeoJson.setAttribute("checked", true);
toggleBtnToGeoJson.addEventListener("click", () => {
toggleToGeoJson(toggleBtnToGeoJson);
});
</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: [121.539489, 25.043539],
pitch: 0,
bearing: 0,
zoom: 14,
maxZoom: 20,
minZoom: 7,
});
// 將藍色線條加入資料層
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: 25,
strokeColor: "#20D0E4",
id: 1,
name: "Bannan Line",
},
});
// 將黃色線條加入資料層
map.data.add({
geometry: new mapPlus.Data.LineString([
{ lng: 121.533018, lat: 25.051893 },
{ lng: 121.532708, lat: 25.033877 },
]),
properties: {
strokeWeight: 25,
strokeColor: "#F0E68C",
id: 1,
name: "Zhonghe-Xinlu Line",
},
});
// 將棕色線條加入資料層
map.data.add({
geometry: new mapPlus.Data.LineString([
{ lng: 121.544062, lat: 25.051843 },
{ lng: 121.543635, lat: 25.03336 },
]),
properties: {
strokeWeight: 25,
strokeColor: "#DEB887",
id: 2,
name: "Wenhu Line",
},
});
/* ══════════════════════════════════════════════════════════════════════════
* 線條圖層隱藏/顯示
══════════════════════════════════════════════════════════════════════════ */
const toggleStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
// 點擊第一個按鈕第一下,隱藏所有線條
map.data.setStyle({ lineVisible: false });
checkboxElement.removeAttribute("checked");
} else {
// 點擊第一個按鈕第二下,顯示所有線條
map.data.setStyle({ lineVisible: true });
checkboxElement.setAttribute("checked", true);
}
};
// 可移除下方註解,以隱藏線條圖層
// map.data.setStyle({ lineVisible: false });
const toggleBtnSetStyle = document.getElementById("toggleBtnSetStyle");
toggleBtnSetStyle.setAttribute("checked", true);
toggleBtnSetStyle.addEventListener("click", () => {
toggleStyle(toggleBtnSetStyle);
});
const toggleToGeoJson = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
// 取得資料層 Geojson 的資料,並顯示內容
map.data.toGeoJson((data) => {
const element = document.getElementById("toGeoJson");
element.style.display = "block";
element.innerHTML = JSON.stringify(data, null, 2);
});
checkboxElement.removeAttribute("checked");
} else {
document.getElementById("toGeoJson").style.display = "none";
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnToGeoJson = document.getElementById("toggleBtnToGeoJson");
toggleBtnToGeoJson.setAttribute("checked", true);
toggleBtnToGeoJson.addEventListener("click", () => {
toggleToGeoJson(toggleBtnToGeoJson);
});
</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;
}
#toGeoJson {
position: absolute;
top: 0;
left: 0;
bottom: 0;
width: 50%;
height: 100%;
overflow: auto;
background: rgba(255, 255, 255, 0.8);
display: none;
}
</style>