我在地图上工作,我根据来自JSON API的数据异步创建GeoJSON图层。我的初始数据加载工作,并查看GeoJSON对象,它似乎添加了额外的元素。
但是,如果我然后尝试使用相同的源(指向更新的GeoJSON对象的数据元素)调用map.getSource(...)。setData(...),我得到
错误:TypeError:无法读取属性' length'未定义的 在Actor.receive(actor.js:77)
我在网上找到的唯一引用与尝试在不再存在的样式上调用setData或具有无效的GeoJSON数据有关,但似乎并非如此 - 例如。
> map.getSource("advertisers");
e {id: "advertisers", type: "geojson", minzoom: 0, maxzoom: 18, tileSize: 512, …}
> features;
(7634) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]
> features[0];
{type: "Feature", geometry: {…}, properties: {…}}
> features[0].geometry;
{type: "Point", coordinates: Array(2)}
> map.getSource("advertisers").setData(features);
e {id: "advertisers", type: "geojson", minzoom: 0, maxzoom: 18, tileSize: 512, …}
evented.js:111 Error: TypeError: Cannot read property 'length' of undefined
at Actor.receive (actor.js:77)
任何建议表示赞赏。
答案 0 :(得分:4)
看起来你正在向setData()
传递一系列功能,但你需要传递一个有效的GeoJSON对象。
您应该替换:map.getSource("advertisers").setData(features);
使用:
map.getSource("advertisers").setData({
type: 'FeatureCollection',
features: features
});