多邊形的增刪
此範例示範如何在地圖上新增及刪除多邊形 Feature,包含以下功能:
- 刪除多邊形(依 ID):使用
map.data.removeFeatureById()方法依據 ID 移除指定的多邊形 - 新增多邊形:使用
map.data.add()方法新增多個 Polygon Feature - 刪除多邊形(依條件):使用
map.data.removeFeature()方法配合條件判斷移除多邊形 - 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="toggleMultiBtnSetStyle"
autocomplete="off"
/>
<label
class="btn btn-outline-primary"
for="toggleMultiBtnSetStyle"
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.5227224646543, lat: 25.02701676669362 },
zoom: 15.8,
});
// Bootstrap 提示訊息 初始化
const tooltipList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
const tooltipInit = [...tooltipList].map(
(tooltip) => new bootstrap.Tooltip(tooltip)
);
// 將左上多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.51774279394641, lat: 25.02830346908604 },
{ lng: 121.51848131881536, lat: 25.027301911647328 },
{ lng: 121.51888179979937, lat: 25.027533732578846 },
{ lng: 121.51909424825834, lat: 25.02752555293422 },
{ lng: 121.51970762723715, lat: 25.027930516565775 },
{ lng: 121.51885490101677, lat: 25.028941308227516 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 0,
},
});
// 將左下多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.51995462852591, lat: 25.025617866336162 },
{ lng: 121.51980338007291, lat: 25.025138193693877 },
{ lng: 121.52038136523652, lat: 25.02489346203282 },
{ lng: 121.52039757042922, lat: 25.024815147797838 },
{ lng: 121.52070006733726, lat: 25.024731938867987 },
{ lng: 121.5207702898328, lat: 25.02494240840406 },
{ lng: 121.52064064830142, lat: 25.0249962493903 },
{ lng: 121.52074868291123, lat: 25.025422081810575 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 1,
},
});
// 將中間多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.5211730858556, lat: 25.02679842059477 },
{ lng: 121.52245995711473, lat: 25.025773483332074 },
{ lng: 121.52260219452722, lat: 25.026567885946108 },
{ lng: 121.52227143457367, lat: 25.027006377834994 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 2,
},
});
// 將右上多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.52376442749033, lat: 25.028484711165802 },
{ lng: 121.52366949650514, lat: 25.027805426509175 },
{ lng: 121.52506256692686, lat: 25.027523673142227 },
{ lng: 121.52514665561011, lat: 25.028383339796434 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 3,
},
});
// 將右下多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.52499224179655, lat: 25.026768269600595 },
{ lng: 121.52482990223632, lat: 25.025040181561437 },
{ lng: 121.52824880309294, lat: 25.02481642414638 },
{ lng: 121.52841649248722, lat: 25.026473113087235 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 4,
},
});
const toggleStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 刪除多邊形(依 ID):
- 可調整 removeFeatureById() 中的 ID 來刪除其他 ID 的多邊形。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第一個按鈕第一下,刪除 ID 為 0 的左上多邊形
map.data.removeFeatureById(0);
checkboxElement.removeAttribute("checked");
} else {
/* ══════════════════════════════════════════════════════════════════════════
* 新增多邊形:
- 可調整 properties 中的 strokeWeight 來改變外框寬度。
- 可調整 properties 中的 fillColor 來改變填色。
- 可調整 properties 中的 strokeColor 來改變外框顏色。
- 可調整 properties 中的 fillOpacity 來改變填色透明度。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第一個按鈕第二下,將 ID 為 0 的左上多邊形加回來
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.51774279394641, lat: 25.02830346908604 },
{ lng: 121.51848131881536, lat: 25.027301911647328 },
{ lng: 121.51888179979937, lat: 25.027533732578846 },
{ lng: 121.51909424825834, lat: 25.02752555293422 },
{ lng: 121.51970762723715, lat: 25.027930516565775 },
{ lng: 121.51885490101677, lat: 25.028941308227516 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 0,
},
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnSetStyle = document.getElementById("toggleBtnSetStyle");
toggleBtnSetStyle.setAttribute("checked", true);
toggleBtnSetStyle.addEventListener("click", () => {
toggleStyle(toggleBtnSetStyle);
});
const toggleMultiStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 刪除多邊形(依條件)
══════════════════════════════════════════════════════════════════════════ */
// 點擊第二個按鈕第一下,一次移除左邊兩個多邊形
map.data.setStyle((fea) => {
if (fea.getProperty("id") < 2) map.data.removeFeature(fea);
});
checkboxElement.removeAttribute("checked");
} else {
// 點擊第二下,將移除的多邊形加回地圖上
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.51774279394641, lat: 25.02830346908604 },
{ lng: 121.51848131881536, lat: 25.027301911647328 },
{ lng: 121.51888179979937, lat: 25.027533732578846 },
{ lng: 121.51909424825834, lat: 25.02752555293422 },
{ lng: 121.51970762723715, lat: 25.027930516565775 },
{ lng: 121.51885490101677, lat: 25.028941308227516 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 0,
},
});
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.51995462852591, lat: 25.025617866336162 },
{ lng: 121.51980338007291, lat: 25.025138193693877 },
{ lng: 121.52038136523652, lat: 25.02489346203282 },
{ lng: 121.52039757042922, lat: 25.024815147797838 },
{ lng: 121.52070006733726, lat: 25.024731938867987 },
{ lng: 121.5207702898328, lat: 25.02494240840406 },
{ lng: 121.52064064830142, lat: 25.0249962493903 },
{ lng: 121.52074868291123, lat: 25.025422081810575 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 1,
},
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleMultiBtnSetStyle = document.getElementById("toggleMultiBtnSetStyle");
toggleMultiBtnSetStyle.setAttribute("checked", true);
toggleMultiBtnSetStyle.addEventListener("click", () => {
toggleMultiStyle(toggleMultiBtnSetStyle);
});
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.5227224646543, lat: 25.02701676669362 },
zoom: 15.8,
});
// Bootstrap 提示訊息 初始化
const tooltipList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
const tooltipInit = [...tooltipList].map(
(tooltip) => new bootstrap.Tooltip(tooltip)
);
// 將左上多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.51774279394641, lat: 25.02830346908604 },
{ lng: 121.51848131881536, lat: 25.027301911647328 },
{ lng: 121.51888179979937, lat: 25.027533732578846 },
{ lng: 121.51909424825834, lat: 25.02752555293422 },
{ lng: 121.51970762723715, lat: 25.027930516565775 },
{ lng: 121.51885490101677, lat: 25.028941308227516 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 0,
},
});
// 將左下多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.51995462852591, lat: 25.025617866336162 },
{ lng: 121.51980338007291, lat: 25.025138193693877 },
{ lng: 121.52038136523652, lat: 25.02489346203282 },
{ lng: 121.52039757042922, lat: 25.024815147797838 },
{ lng: 121.52070006733726, lat: 25.024731938867987 },
{ lng: 121.5207702898328, lat: 25.02494240840406 },
{ lng: 121.52064064830142, lat: 25.0249962493903 },
{ lng: 121.52074868291123, lat: 25.025422081810575 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 1,
},
});
// 將中間多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.5211730858556, lat: 25.02679842059477 },
{ lng: 121.52245995711473, lat: 25.025773483332074 },
{ lng: 121.52260219452722, lat: 25.026567885946108 },
{ lng: 121.52227143457367, lat: 25.027006377834994 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 2,
},
});
// 將右上多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.52376442749033, lat: 25.028484711165802 },
{ lng: 121.52366949650514, lat: 25.027805426509175 },
{ lng: 121.52506256692686, lat: 25.027523673142227 },
{ lng: 121.52514665561011, lat: 25.028383339796434 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 3,
},
});
// 將右下多邊形加進資料層
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.52499224179655, lat: 25.026768269600595 },
{ lng: 121.52482990223632, lat: 25.025040181561437 },
{ lng: 121.52824880309294, lat: 25.02481642414638 },
{ lng: 121.52841649248722, lat: 25.026473113087235 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 4,
},
});
const toggleStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 刪除多邊形(依 ID):
- 可調整 removeFeatureById() 中的 ID 來刪除其他 ID 的多邊形。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第一個按鈕第一下,刪除 ID 為 0 的左上多邊形
map.data.removeFeatureById(0);
checkboxElement.removeAttribute("checked");
} else {
/* ══════════════════════════════════════════════════════════════════════════
* 新增多邊形:
- 可調整 properties 中的 strokeWeight 來改變外框寬度。
- 可調整 properties 中的 fillColor 來改變填色。
- 可調整 properties 中的 strokeColor 來改變外框顏色。
- 可調整 properties 中的 fillOpacity 來改變填色透明度。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第一個按鈕第二下,將 ID 為 0 的左上多邊形加回來
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.51774279394641, lat: 25.02830346908604 },
{ lng: 121.51848131881536, lat: 25.027301911647328 },
{ lng: 121.51888179979937, lat: 25.027533732578846 },
{ lng: 121.51909424825834, lat: 25.02752555293422 },
{ lng: 121.51970762723715, lat: 25.027930516565775 },
{ lng: 121.51885490101677, lat: 25.028941308227516 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 0,
},
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnSetStyle = document.getElementById("toggleBtnSetStyle");
toggleBtnSetStyle.setAttribute("checked", true);
toggleBtnSetStyle.addEventListener("click", () => {
toggleStyle(toggleBtnSetStyle);
});
const toggleMultiStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 刪除多邊形(依條件)
══════════════════════════════════════════════════════════════════════════ */
// 點擊第二個按鈕第一下,一次移除左邊兩個多邊形
map.data.setStyle((fea) => {
if (fea.getProperty("id") < 2) map.data.removeFeature(fea);
});
checkboxElement.removeAttribute("checked");
} else {
// 點擊第二下,將移除的多邊形加回地圖上
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.51774279394641, lat: 25.02830346908604 },
{ lng: 121.51848131881536, lat: 25.027301911647328 },
{ lng: 121.51888179979937, lat: 25.027533732578846 },
{ lng: 121.51909424825834, lat: 25.02752555293422 },
{ lng: 121.51970762723715, lat: 25.027930516565775 },
{ lng: 121.51885490101677, lat: 25.028941308227516 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 0,
},
});
map.data.add({
geometry: new mapPlus.Data.Polygon([
[
{ lng: 121.51995462852591, lat: 25.025617866336162 },
{ lng: 121.51980338007291, lat: 25.025138193693877 },
{ lng: 121.52038136523652, lat: 25.02489346203282 },
{ lng: 121.52039757042922, lat: 25.024815147797838 },
{ lng: 121.52070006733726, lat: 25.024731938867987 },
{ lng: 121.5207702898328, lat: 25.02494240840406 },
{ lng: 121.52064064830142, lat: 25.0249962493903 },
{ lng: 121.52074868291123, lat: 25.025422081810575 },
],
]),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 1,
},
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleMultiBtnSetStyle = document.getElementById("toggleMultiBtnSetStyle");
toggleMultiBtnSetStyle.setAttribute("checked", true);
toggleMultiBtnSetStyle.addEventListener("click", () => {
toggleMultiStyle(toggleMultiBtnSetStyle);
});
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>