2017-10-10 72 views
0

我正在製作一個地圖,我正在根據數據異步創建GeoJSON圖層一個JSON API。我的初始數據加載工作,看着GeoJSON對象,它似乎得到添加額外的元素。調用map.getSource(...)。setData(...)導致TypeError:無法讀取未定義的屬性「長度」

但是,如果我再嘗試撥打map.getSource(...)。使用setData(...)具有相同源(指向更新以GeoJSON對象中的數據元素),我得到

錯誤:類型錯誤:無法讀取性能在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) 

任何建議表示讚賞。

回答

2

看起來您正在向setData()傳遞一組功能,但您需要傳遞有效的GeoJSON對象。

,就應該替換:map.getSource("advertisers").setData(features);

有:

map.getSource("advertisers").setData({ 
    type: 'FeatureCollection', 
    features: features 
}); 
+0

*捂臉*謝謝,我想我已經在相同的代碼過長一直盯着。那樣做了。 –

相關問題