【发布时间】:2022-04-10 14:44:23
【问题描述】:
我已经从 mapbox 复制并调整了这个示例: https://docs.mapbox.com/mapbox-gl-js/example/filter-markers/
一切正常,但我希望将 geojson 作为外部文件。
所以我更改了这段代码:
var places = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'properties': {
'icon': 'theatre'
},
'geometry': {
'type': 'Point',
'coordinates': [-77.038659, 38.931567]
}
},
]
};
用这个:
var places = '../images/destinations.geojson';
我在 DevTools 中收到此错误:未捕获的类型错误:无法读取未定义的属性“forEach”。
其余的代码(我得到错误)是这样的:
map.on('load', function() {
// Add a GeoJSON source containing place coordinates and information.
map.addSource('places', {
'type': 'geojson',
'data': places
});
places.features.forEach(function(feature) {
var symbol = feature.properties['icon'];
var layerID = 'poi-' + symbol;
// Add a layer for this symbol type if it hasn't been added already.
if (!map.getLayer(layerID)) {
map.addLayer({
'id': layerID,
'type': 'symbol',
'source': 'places',
'layout': {
'icon-image': symbol + '-15',
'icon-allow-overlap': true
},
'filter': ['==', 'icon', symbol]
});
【问题讨论】: