Can';无法从外部geojson到mapbox获取属性

Can';无法从外部geojson到mapbox获取属性,mapbox,geojson,Mapbox,Geojson,我已从mapbox复制并调整了此示例: 一切正常,但我希望将geojson作为外部文件 所以我修改了这个代码: var places = { 'type': 'FeatureCollection', 'features': [ { 'type': 'Feature', 'properties': { 'icon': 'theatre' }, 'geometry': { 'type': 'Point', 'coordinates': [-77.038659, 38.931567] } }, ]

我已从mapbox复制并调整了此示例:

一切正常,但我希望将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中得到了这个错误:uncaughttypeerror:无法读取未定义的属性'forEach'

其余代码(我得到错误)如下所示:


加载外部geoJSON文件的一种方法是使用d3js。请参见下面的示例,该示例取自。本例将为geoJSON文件中给出的坐标画一条线

您应该能够使用forEach循环迭代
数据.功能


从外部geoJSON加载
正文{margin:0;padding:0;}
#映射{位置:绝对;顶部:0;底部:0;宽度:100%;}
mapboxgl.accessToken='';
var map=new mapboxgl.map({
容器:“映射”,
风格:'mapbox://styles/mapbox/satellite-v9',
缩放:0
});
map.on('load',function()){
//我们在这里使用D3获取JSON,这样我们就可以单独解析和使用它
//在添加的源代码中使用GL JS。您可以使用任何请求方法(库
//或者其他)你想要的。
d3.json(
'https://docs.mapbox.com/mapbox-gl-js/assets/hike.geojson“,//geoJSON数据文件
功能(错误、数据){
如果(错误)抛出错误;
//保存完整的坐标列表供以后使用
var坐标=数据。要素[0]。几何体。坐标;
//首先只显示第一个坐标
data.features[0].geometry.coordinates=[coordinates[0]];
//把它添加到地图上
addSource('trace',{type:'geojson',data:data});
map.addLayer({
'id':'trace',
“类型”:“行”,
“源”:“跟踪”,
“油漆”:{
“线条颜色”:“黄色”,
“线不透明度”:0.75,
“线宽”:5
}
});
}
);
});

是!成功了。如果要链接到已导入mapbox的数据集,语法是什么?
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]
});