jce*_*has 2 mapbox mapbox-gl mapbox-gl-js
我有一张包含几十层的地图,每层都有一个唯一的 ID。我有用于打开和关闭图层的复选框,为此我需要所有图层 ID 的单个数组。我不知道如何循环遍历所有地图图层来捕获图层 ID。我尝试使用map.getLayer(),但这将图层作为对象返回,而不是将图层 ID 作为字符串返回。我想循环遍历所有地图图层并将图层 ID 字符串推送到新数组。我该怎么做呢?
mapboxgl.accessToken = "myaccesstoken";
var map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/mymapboxstyle",
center: [-71.0664, 42.358],
minZoom: 14 //
});
map.on("style.load", function () {
map.addSource("contours", {
type: "vector",
url: "mapbox://mapbox.mapbox-terrain-v2"
});
map.addSource("hDistricts-2017", {
"type": "vector",
"url": "mapbox://mysource"
});
map.addLayer({
"id": "contours",
"type": "line",
"source": "contours",
"source-layer": "contour",
"layout": {
"visibility": "none",
"line-join": "round",
"line-cap": "round"
},
"paint": {
"line-color": "#877b59",
"line-width": 1
}
});
map.addLayer({
"id": "Back Bay Architectural District",
"source": "hDistricts-2017",
"source-layer": "Boston_Landmarks_Commission_B-7q48wq",
"type": "fill",
"layout": {
"visibility": "none"
},
"filter": ["==", "OBJECTID", 13],
"paint": {
"fill-color": "#192E39",
"fill-outline-color": "#000000",
"fill-opacity": 0.5
}
});
});
var layerIds = [];
function getIds() {
//here I need to iterate through map layers to get id strings.
//how do I do this???
layerIds.push( ); //then push those ids to new array.
console.log(layerIds); //["contours", "Back Bay Architectural District"]
}
Run Code Online (Sandbox Code Playgroud)
如果由于未知原因,kielni 答案不方便,请使用map.getStyle().layers获取对象层数组,然后将其映射以获取字符串 id 数组。
var layers = map.getStyle().layers;
var layerIds = layers.map(function (layer) {
return layer.id;
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7521 次 |
| 最近记录: |