圓形增加文字
此範例示範如何在地圖上為圓形 Feature 增加文字顯示,包含以下功能:
- 增加/移除文字說明:使用
displayFeatureText()方法將文字顯示在圓形中心,使用removeDisplayText()方法移除顯示的文字 - 變更文字來源:透過
displayFeatureText()指定顯示固定字串或屬性名稱 - 變更文字樣式:使用
map.data.setStyle()方法變更文字大小及顏色
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;
}
</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-file-minus-fill"></i>
</label>
</div>
<div>
<input
type="checkbox"
class="btn-check"
id="toggleBtnSetOtherText"
autocomplete="off"
/>
<label
class="btn btn-outline-primary"
for="toggleBtnSetOtherText"
data-bs-toggle="tooltip"
data-bs-placement="left"
data-bs-title="變更文字來源"
>
<i class="bi bi-calendar3-range-fill"></i>
</label>
</div>
<div>
<input
type="checkbox"
class="btn-check"
id="toggleBtnLayerStyle"
autocomplete="off"
/>
<label
class="btn btn-outline-primary"
for="toggleBtnLayerStyle"
data-bs-toggle="tooltip"
data-bs-placement="left"
data-bs-title="變更文字樣式"
>
<i class="bi bi-gear-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"]');
[...tooltipList].map((tooltip) => new bootstrap.Tooltip(tooltip));
// 將左上圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.518793,
lat: 25.027923,
radius: 80,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 0,
},
});
// 將左下圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.520326,
lat: 25.025145,
radius: 70,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 1,
},
});
// 將中間圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.522127,
lat: 25.026536,
radius: 60,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 2,
},
});
// 將右上圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.524411,
lat: 25.02805,
radius: 75,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 3,
},
});
// 將右下圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.526622,
lat: 25.025525,
radius: 120,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 4,
},
});
const toggleStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 增加文字說明:
- 可調整 displayFeatureText() 中的參數,改顯示不同的文字。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第一個按鈕第一下,將文字說明加至圓形上方
map.data.setStyle((feature) => {
// 左上角的圓形文字顯示『K』,其餘顯示各自的 id
if (feature.properties.id === 0) feature.displayFeatureText("K");
else feature.displayFeatureText("id");
// 圓形文字顯示『A』
// feature.displayFeatureText("A");
});
checkboxElement.removeAttribute("checked");
} else {
// 點擊第一個按鈕第二下,移除顯示的文字
map.data.setStyle((feature) => {
feature.removeDisplayText();
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnSetStyle = document.getElementById("toggleBtnSetStyle");
toggleBtnSetStyle.setAttribute("checked", true);
toggleBtnSetStyle.addEventListener("click", () => {
toggleStyle(toggleBtnSetStyle);
});
const toggleSetOtherText = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 變更文字來源
══════════════════════════════════════════════════════════════════════════ */
// 點擊第二個按鈕第一下,將文字說明加至圓形上方
map.data.setStyle((feature) => {
// 抓取『fillColor』的屬性欄位
feature.displayFeatureText("fillColor");
});
checkboxElement.removeAttribute("checked");
} else {
map.data.setStyle((feature) => {
// 點擊第二個按鈕第二下,移除顯示的文字
feature.removeDisplayText();
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnSetOtherText = document.getElementById("toggleBtnSetOtherText");
toggleBtnSetOtherText.setAttribute("checked", true);
toggleBtnSetOtherText.addEventListener("click", () => {
toggleSetOtherText(toggleBtnSetOtherText);
});
const toggleLayerStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
// 顯示文字在圓形上
map.data.setStyle((feature) => {
if (feature.properties.id === 0) feature.displayFeatureText("K");
else feature.displayFeatureText("id");
});
/* ══════════════════════════════════════════════════════════════════════════
* 變更文字樣式:
- 可調整文字大小及文字顏色。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第三個按鈕第一下
map.data.setStyle((feature) => {
// 左側兩個圓形文字大小修改為 25,其餘則修改為 20
const size = feature.getProperty("id") < 2 ? 25 : 20;
// 左側兩個圓形文字大小修改為 20,其餘則修改為 15
// const size = feature.getProperty("id") < 2 ? 20 : 15;
// 左側兩個圓形文字顏色修改為藍色,其餘則修改為綠色
const color = feature.getProperty("id") < 2 ? "blue" : "darkgreen";
// 左側兩個圓形文字顏色修改為橘色,其餘則修改為灰色
// const color = feature.getProperty("id") < 2 ? "orange" : "grey";
return {
textSize: size,
textColor: color,
};
});
checkboxElement.removeAttribute("checked");
} else {
// 恢復文字的原始狀態
map.data.setStyle({ textColor: "black", textSize: 14 });
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnLayerStyle = document.getElementById("toggleBtnLayerStyle");
toggleBtnLayerStyle.setAttribute("checked", true);
toggleBtnLayerStyle.addEventListener("click", () => {
toggleLayerStyle(toggleBtnLayerStyle);
});
</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"]');
[...tooltipList].map((tooltip) => new bootstrap.Tooltip(tooltip));
// 將左上圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.518793,
lat: 25.027923,
radius: 80,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 0,
},
});
// 將左下圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.520326,
lat: 25.025145,
radius: 70,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 1,
},
});
// 將中間圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.522127,
lat: 25.026536,
radius: 60,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 2,
},
});
// 將右上圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.524411,
lat: 25.02805,
radius: 75,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 3,
},
});
// 將右下圓形加進資料層
map.data.add({
geometry: new mapPlus.Data.Circle({
lng: 121.526622,
lat: 25.025525,
radius: 120,
}),
properties: {
strokeWeight: 5,
fillColor: "#F0E68C",
strokeColor: "#DAA520",
fillOpacity: 1,
id: 4,
},
});
const toggleStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 增加文字說明:
- 可調整 displayFeatureText() 中的參數,改顯示不同的文字。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第一個按鈕第一下,將文字說明加至圓形上方
map.data.setStyle((feature) => {
// 左上角的圓形文字顯示『K』,其餘顯示各自的 id
if (feature.properties.id === 0) feature.displayFeatureText("K");
else feature.displayFeatureText("id");
// 圓形文字顯示『A』
// feature.displayFeatureText("A");
});
checkboxElement.removeAttribute("checked");
} else {
// 點擊第一個按鈕第二下,移除顯示的文字
map.data.setStyle((feature) => {
feature.removeDisplayText();
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnSetStyle = document.getElementById("toggleBtnSetStyle");
toggleBtnSetStyle.setAttribute("checked", true);
toggleBtnSetStyle.addEventListener("click", () => {
toggleStyle(toggleBtnSetStyle);
});
const toggleSetOtherText = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 變更文字來源
══════════════════════════════════════════════════════════════════════════ */
// 點擊第二個按鈕第一下,將文字說明加至圓形上方
map.data.setStyle((feature) => {
// 抓取『fillColor』的屬性欄位
feature.displayFeatureText("fillColor");
});
checkboxElement.removeAttribute("checked");
} else {
map.data.setStyle((feature) => {
// 點擊第二個按鈕第二下,移除顯示的文字
feature.removeDisplayText();
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnSetOtherText = document.getElementById("toggleBtnSetOtherText");
toggleBtnSetOtherText.setAttribute("checked", true);
toggleBtnSetOtherText.addEventListener("click", () => {
toggleSetOtherText(toggleBtnSetOtherText);
});
const toggleLayerStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
// 顯示文字在圓形上
map.data.setStyle((feature) => {
if (feature.properties.id === 0) feature.displayFeatureText("K");
else feature.displayFeatureText("id");
});
/* ══════════════════════════════════════════════════════════════════════════
* 變更文字樣式:
- 可調整文字大小及文字顏色。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第三個按鈕第一下
map.data.setStyle((feature) => {
// 左側兩個圓形文字大小修改為 25,其餘則修改為 20
const size = feature.getProperty("id") < 2 ? 25 : 20;
// 左側兩個圓形文字大小修改為 20,其餘則修改為 15
// const size = feature.getProperty("id") < 2 ? 20 : 15;
// 左側兩個圓形文字顏色修改為藍色,其餘則修改為綠色
const color = feature.getProperty("id") < 2 ? "blue" : "darkgreen";
// 左側兩個圓形文字顏色修改為橘色,其餘則修改為灰色
// const color = feature.getProperty("id") < 2 ? "orange" : "grey";
return {
textSize: size,
textColor: color,
};
});
checkboxElement.removeAttribute("checked");
} else {
// 恢復文字的原始狀態
map.data.setStyle({ textColor: "black", textSize: 14 });
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnLayerStyle = document.getElementById("toggleBtnLayerStyle");
toggleBtnLayerStyle.setAttribute("checked", true);
toggleBtnLayerStyle.addEventListener("click", () => {
toggleLayerStyle(toggleBtnLayerStyle);
});
</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>