線條增加 Icon 顯示
此範例示範如何在地圖上實現線條 Feature 增加 Icon 顯示,包含以下功能:
- 沿線條增加 Icon:使用
iconAlongLinestring()方法在線條上增加 Icon 顯示 - 載入外部圖片:使用
map.loadImage()方法載入外部 URL 圖片作為 Icon - 調整旋轉角度:使用
map.data.setStyle()方法動態調整 Icon 的旋轉角度 - 變更 Icon 圖案:使用
iconAlongLinestring()搭配iconImage屬性變更 Icon 圖案
info
詳細使用說明請參考「資料層」。
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>線條增加Icon顯示</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="變更藍色線條上 Icon 的旋轉角度"
>
<i class="bi bi-forward-fill"></i>
</label>
</div>
<div>
<input
type="checkbox"
class="btn-check"
id="toggleBtnSetImage"
autocomplete="off"
/>
<label
class="btn btn-outline-primary"
for="toggleBtnSetImage"
data-bs-toggle="tooltip"
data-bs-placement="left"
data-bs-title="變更棕色線條上的 Icon"
>
<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.539489, lat: 25.043539 },
zoom: 14,
});
// 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,
},
});
// 將黃色線條加進資料層
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,
},
});
// 將棕色線條加進資料層
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,
},
});
/* ══════════════════════════════════════════════════════════════════════════
* 沿線條增加 Icon:
- 可調整 rotationAlignmentMap,設置 icon 的垂直目標。
══════════════════════════════════════════════════════════════════════════ */
map.data.setStyle((fea) => {
fea.iconAlongLinestring({
// icon 的垂直目標,true 為垂直線條方向,false 為垂直地圖視窗方向
rotationAlignmentMap: false,
});
});
/* ══════════════════════════════════════════════════════════════════════════
* 載入外部圖片
══════════════════════════════════════════════════════════════════════════ */
map.loadImage({
png: "https://kw3dmap.localking.com.tw/3dmap/img/kingway.png",
imageName: "newImage",
});
const toggleStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 調整旋轉角度:
- 可調整 iconRotate,更改 icon 的旋轉角度。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第一個按鈕第一下,動態調整 ID 為 1 的藍色線條上 icon 的旋轉角度
map.data.setStyle((fea) => {
// 順時鐘旋轉 90 度
if (fea.getProperty("id") === 1) return { iconRotate: 90 };
// 順時鐘旋轉 30 度
// if (fea.getProperty("id") === 1) return { iconRotate: 30 };
});
checkboxElement.removeAttribute("checked");
} else {
// 回復座標點原始設定
map.data.setStyle((fea) => {
if (fea.getProperty("id") === 1) return { iconRotate: 0 };
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnSetStyle = document.getElementById("toggleBtnSetStyle");
toggleBtnSetStyle.setAttribute("checked", true);
toggleBtnSetStyle.addEventListener("click", () => {
toggleStyle(toggleBtnSetStyle);
});
const toggleImage = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 變更 Icon 圖案:
- 可調整 iconSize,設置 icon 的大小。
- 可調整 rotationAlignmentMap,設置 icon 的垂直目標。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第二個按鈕第一下,變更 ID 為 3 的棕色線條上 Icon 圖案
map.data.setStyle((fea) => {
if (fea.getProperty("id") === 3) {
fea.iconAlongLinestring({
// icon 圖案
iconImage: "newImage",
// icon 大小
iconSize: 0.1,
// icon 的垂直目標,true 為垂直線條方向,false 為垂直地圖視窗方向
rotationAlignmentMap: false,
});
}
});
checkboxElement.removeAttribute("checked");
} else {
// 回復座標點原始設定
map.data.setStyle((fea) => {
if (fea.getProperty("id") === 3) {
fea.iconAlongLinestring({
rotationAlignmentMap: false, // 垂直於地圖視窗方向
});
}
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnSetImage = document.getElementById("toggleBtnSetImage");
toggleBtnSetImage.setAttribute("checked", true);
toggleBtnSetImage.addEventListener("click", () => {
toggleImage(toggleBtnSetImage);
});
</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"]');
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,
},
});
// 將黃色線條加進資料層
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,
},
});
// 將棕色線條加進資料層
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,
},
});
/* ══════════════════════════════════════════════════════════════════════════
* 沿線條增加 Icon:
- 可調整 rotationAlignmentMap,設置 icon 的垂直目標。
══════════════════════════════════════════════════════════════════════════ */
map.data.setStyle((fea) => {
fea.iconAlongLinestring({
// icon 的垂直目標,true 為垂直線條方向,false 為垂直地圖視窗方向
rotationAlignmentMap: false,
});
});
/* ══════════════════════════════════════════════════════════════════════════
* 載入外部圖片
══════════════════════════════════════════════════════════════════════════ */
map.loadImage({
png: "https://kw3dmap.localking.com.tw/3dmap/img/kingway.png",
imageName: "newImage",
});
const toggleStyle = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 調整旋轉角度:
- 可調整 iconRotate,更改 icon 的旋轉角度。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第一個按鈕第一下,動態調整 ID 為 1 的藍色線條上 icon 的旋轉角度
map.data.setStyle((fea) => {
// 順時鐘旋轉 90 度
if (fea.getProperty("id") === 1) return { iconRotate: 90 };
// 順時鐘旋轉 30 度
// if (fea.getProperty("id") === 1) return { iconRotate: 30 };
});
checkboxElement.removeAttribute("checked");
} else {
// 回復座標點原始設定
map.data.setStyle((fea) => {
if (fea.getProperty("id") === 1) return { iconRotate: 0 };
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnSetStyle = document.getElementById("toggleBtnSetStyle");
toggleBtnSetStyle.setAttribute("checked", true);
toggleBtnSetStyle.addEventListener("click", () => {
toggleStyle(toggleBtnSetStyle);
});
const toggleImage = (checkboxElement) => {
if (checkboxElement.getAttribute("checked")) {
/* ══════════════════════════════════════════════════════════════════════════
* 變更 Icon 圖案:
- 可調整 iconSize,設置 icon 的大小。
- 可調整 rotationAlignmentMap,設置 icon 的垂直目標。
══════════════════════════════════════════════════════════════════════════ */
// 點擊第二個按鈕第一下,變更 ID 為 3 的棕色線條上 Icon 圖案
map.data.setStyle((fea) => {
if (fea.getProperty("id") === 3) {
fea.iconAlongLinestring({
// icon 圖案
iconImage: "newImage",
// icon 大小
iconSize: 0.1,
// icon 的垂直目標,true 為垂直線條方向,false 為垂直地圖視窗方向
rotationAlignmentMap: false,
});
}
});
checkboxElement.removeAttribute("checked");
} else {
// 回復座標點原始設定
map.data.setStyle((fea) => {
if (fea.getProperty("id") === 3) {
fea.iconAlongLinestring({
rotationAlignmentMap: false, // 垂直於地圖視窗方向
});
}
});
checkboxElement.setAttribute("checked", true);
}
};
const toggleBtnSetImage = document.getElementById("toggleBtnSetImage");
toggleBtnSetImage.setAttribute("checked", true);
toggleBtnSetImage.addEventListener("click", () => {
toggleImage(toggleBtnSetImage);
});
</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>