Feature 的顯示狀態
此範例示範如何控制整個資料層的顯示狀態,並查看資料層輸出的 GeoJSON,包含以下功能:
- 資料層隱藏/顯示:使用
map.data.setStyle()方法設定visible: false/true隱藏/顯示所有資料層 - GeoJSON 查看:使用
map.data.toGeoJson()查看整個資料層的 GeoJSON 格式內容
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>
<!-- 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%;
overflow: auto;
background: rgba(255, 255, 255, 0.8);
display: none;
margin: 0;
padding: 12px;
}
</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-hexagon-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">
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,
});
// Bootstrap 提示訊息初始化
const tooltipList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
[...tooltipList].map((tooltip) => new bootstrap.Tooltip(tooltip));
// 將藍色線條加入資料層
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: 10,
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: 10,
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: 10,
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,
},
});
/* ══════════════════════════════════════════════════════════════════════════
* 隱藏/顯示資料層
══════════════════════════════════════════════════════════════════════════ */
const toggleStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
// 點擊第一個按鈕第一下,隱藏資料層
map.data.setStyle({ visible: false });
checkboxElement.removeAttribute("checked");
} else {
// 點擊第一個按鈕第二下,顯示資料層
map.data.setStyle({ visible: true });
checkboxElement.setAttribute("checked", true);
}
};
// 可移除下方註解,以隱藏所有資料層 feature
// map.data.setStyle({ visible: 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">
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,
});
// Bootstrap 提示訊息初始化
const tooltipList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
[...tooltipList].map((tooltip) => new bootstrap.Tooltip(tooltip));
// 將藍色線條加入資料層
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: 10,
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: 10,
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: 10,
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,
},
});
/* ══════════════════════════════════════════════════════════════════════════
* 隱藏/顯示資料層
══════════════════════════════════════════════════════════════════════════ */
const toggleStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
// 點擊第一個按鈕第一下,隱藏資料層
map.data.setStyle({ visible: false });
checkboxElement.removeAttribute("checked");
} else {
// 點擊第一個按鈕第二下,顯示資料層
map.data.setStyle({ visible: true });
checkboxElement.setAttribute("checked", true);
}
};
// 可移除下方註解,以隱藏所有資料層 feature
// map.data.setStyle({ visible: 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%;
overflow: auto;
background: rgba(255, 255, 255, 0.8);
display: none;
margin: 0;
padding: 12px;
}
</style>