線條增加文字
此範例示範如何在地圖上為線條 Feature 增加文字顯示,包含以下功能:
- 增加/移除文字說明:使用
displayFeatureText()方法將文字說明加至線條上方,使用removeDisplayText()方法移除顯示的文字 - 變更文字來源:透過
displayFeatureText()可接收字串或屬性名稱來顯示對應內容 - 變更文字樣式:使用
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>
<!-- mapPlus 地圖容器 -->
<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="toggleBtnSetBlueStyle"
autocomplete="off"
/>
<label
class="btn btn-outline-primary"
for="toggleBtnSetBlueStyle"
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="toggleBtnSetLineTextLayout"
autocomplete="off"
/>
<label
class="btn btn-outline-primary"
for="toggleBtnSetLineTextLayout"
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.538489, lat: 25.042539 },
zoom: 14.5,
});
// 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.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,
name: "板南線",
},
});
// 將黃色線條加進資料層
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,
name: "中和新蘆線",
},
});
// 將棕色線條加進資料層
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,
name: "文湖線",
},
});
const toggleStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 增加文字說明:
- 可調整 displayFeatureText() 中的參數,改顯示不同的文字。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第一個按鈕第一下,將文字說明加至線條上方
map.data.setStyle((fea) => {
if (fea.properties.id === 1) {
// 橫向藍色線條顯示『BLUE』文字說明
fea.displayFeatureText("BLUE");
// 橫向藍色線條顯示『藍藍的』文字說明
// fea.displayFeatureText("藍藍的");
} else {
// 直向線條抓取『name』的屬性欄位
fea.displayFeatureText("name");
// 直向線條抓取『id』的屬性欄位
// fea.displayFeatureText("id");
}
});
checkboxElement.removeAttribute("checked");
} else {
// 點擊第一個按鈕第二下,移除顯示的文字
map.data.setStyle((fea) => {
fea.removeDisplayText();
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnSetBlueStyle = document.getElementById("toggleBtnSetBlueStyle");
toggleBtnSetBlueStyle.setAttribute("checked", true);
toggleBtnSetBlueStyle.addEventListener("click", () => {
toggleStyle(toggleBtnSetBlueStyle);
});
const toggleSetLineTextLayout = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 變更文字來源
══════════════════════════════════════════════════════════════════════════ */
// 點擊第二個按鈕第一下,將文字說明加至線條上方
map.data.setStyle((fea) => {
// 抓取『strokeColor』的屬性欄位
fea.displayFeatureText("strokeColor");
});
checkboxElement.removeAttribute("checked");
} else {
map.data.setStyle((fea) => {
// 點擊第二個按鈕第二下,移除顯示的文字
fea.removeDisplayText();
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnSetLineTextLayout = document.getElementById(
"toggleBtnSetLineTextLayout"
);
toggleBtnSetLineTextLayout.setAttribute("checked", true);
toggleBtnSetLineTextLayout.addEventListener("click", () => {
toggleSetLineTextLayout(toggleBtnSetLineTextLayout);
});
const toggleLayerStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
// 顯示文字在線條上
map.data.setStyle((fea) => {
if (fea.properties.id === 1) {
fea.displayFeatureText("BLUE");
} else {
fea.displayFeatureText("name");
}
});
/* ══════════════════════════════════════════════════════════════════════════
* 變更文字樣式:
- 可調整文字大小及文字顏色。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第三個按鈕第一下
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.538489, lat: 25.042539 },
zoom: 14.5,
});
// 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.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,
name: "板南線",
},
});
// 將黃色線條加進資料層
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,
name: "中和新蘆線",
},
});
// 將棕色線條加進資料層
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,
name: "文湖線",
},
});
const toggleStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 增加文字說明:
- 可調整 displayFeatureText() 中的參數,改顯示不同的文字。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第一個按鈕第一下,將文字說明加至線條上方
map.data.setStyle((fea) => {
if (fea.properties.id === 1) {
// 橫向藍色線條顯示『BLUE』文字說明
fea.displayFeatureText("BLUE");
// 橫向藍色線條顯示『藍藍的』文字說明
// fea.displayFeatureText("藍藍的");
} else {
// 直向線條抓取『name』的屬性欄位
fea.displayFeatureText("name");
// 直向線條抓取『id』的屬性欄位
// fea.displayFeatureText("id");
}
});
checkboxElement.removeAttribute("checked");
} else {
// 點擊第一個按鈕第二下,移除顯示的文字
map.data.setStyle((fea) => {
fea.removeDisplayText();
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnSetBlueStyle = document.getElementById("toggleBtnSetBlueStyle");
toggleBtnSetBlueStyle.setAttribute("checked", true);
toggleBtnSetBlueStyle.addEventListener("click", () => {
toggleStyle(toggleBtnSetBlueStyle);
});
const toggleSetLineTextLayout = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 變更文字來源
══════════════════════════════════════════════════════════════════════════ */
// 點擊第二個按鈕第一下,將文字說明加至線條上方
map.data.setStyle((fea) => {
// 抓取『strokeColor』的屬性欄位
fea.displayFeatureText("strokeColor");
});
checkboxElement.removeAttribute("checked");
} else {
map.data.setStyle((fea) => {
// 點擊第二個按鈕第二下,移除顯示的文字
fea.removeDisplayText();
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnSetLineTextLayout = document.getElementById(
"toggleBtnSetLineTextLayout"
);
toggleBtnSetLineTextLayout.setAttribute("checked", true);
toggleBtnSetLineTextLayout.addEventListener("click", () => {
toggleSetLineTextLayout(toggleBtnSetLineTextLayout);
});
const toggleLayerStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
// 顯示文字在線條上
map.data.setStyle((fea) => {
if (fea.properties.id === 1) {
fea.displayFeatureText("BLUE");
} else {
fea.displayFeatureText("name");
}
});
/* ══════════════════════════════════════════════════════════════════════════
* 變更文字樣式:
- 可調整文字大小及文字顏色。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第三個按鈕第一下
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>