Leaflet.timeline geojson解析错误

时间:2018-03-12 02:13:13

标签: javascript leaflet geojson

尝试实施Leaflet.timeline并遇到GeoJSON结构或其他问题。

为了简化调试,我已将geojson添加为var(实际上它是由Rails动态创建的)。这是开始:

 var data = [{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"start":"1903","end":"1908","name":"part of S. Toluca St. (26). and second block south gone","title":"Was Brook before 1903 and now is part of S. Toluca St. (26). and second block south gone 1908.","link=":91},"geometry":{"type":"LineString","coordinates":[[-118.25862396508458,34.06087254304104],[-118.25933206826451,34.05994816216629]]}},{"type":"Feature","properties":{"start":"1903","end":"1928","name":"part of E. 40th Place","title":"Was 37th St before 1903 and now is part of E. 40th Place 1928.","link=":650},"geometry":{"type":"LineString","coordinates":[[-118.25712423772116,34.01007010760971],[-118.25649456380442,34.01016443793837],[-118.25584971702219,34.01016443793837],[-118.25427932544667,34.0102021700405],[-118.25213995141625,34.010227324765935]]}},{"type":"Feature","properties":{"start":"1903","end":"1908","name":"part of E. 9th Place (21).","title":"Was 10th St.  before 1903 and now is part of E. 9th Place (21). 1908.","link=":3},"geometry":{"type":"LineString","coordinates":[[-118.24982816353442,34.035546195508864],[-118.25104052200915,34.03663976724366]]}},{"type":"Feature","properties":{"start":"1921","end":"","name":"Miramar St. One block abandoned","title":"Was 3rd St before 1921 and now is Miramar St. One block abandoned .","link=":645},"geometry":{"type":"LineString","coordinates":[[-118.26117539280003,34.05901974362122],[-118.2593849946753,34.05823410691563],[-118.25815599257271,34.05768101430694],[-118.25759459655055,34.05717191451128],[-118.25663111959356,34.05654339202722]]}},{"type":"Feature","properties":{"start":"1928","end":"1930","name":"Commonwealth Pl and a portion abandoned","title":"Was Auto Pl before 1928 and now is Commonwealth Pl and a portion abandoned 1930.","link=":50},"geometry":{"type":"LineString","coordinates":[[-118.28558737412096,34.07543021182353],...

我来到Leaflet.timeline:

_process(data) {
  data.features.forEach((feature) => {

查看data

data screen shot

换句话说,它似乎是完整和理解的。如果从这里运行它会失败Uncaught TypeError: Cannot read property 'forEach' of undefined,所以再次重新加载并停止;并在控制台data[0].features

result of data{0}.features

控制台中的

和最后data[0].features.forEach(feature)

result of data[0].features.forEach(feature)

我做错了什么或我应该检查什么? geojson与var segmentLayer = L.mapbox.featureLayer().loadURL('overview_data.geojson').addTo(laMap);一起使用,但我想添加一个时间滑块。

不直接回答,但我解决了阅读GeoJSON的更大问题。正如我在Reading a GeoJSON file into Rails/Leaflet中所说,我用

读了GeoJSON
$.getJSON("overview/overview_data.json", function (data) {
   <all the stuff I needed to to>
} )

虽然它看起来一样,但我必须对GeoJSON进行一些重组,因为这里提到的问题已经消失了。

1 个答案:

答案 0 :(得分:0)

很可能你不应该将你的Feature Collection包装在一个数组中。

虽然Leaflet的L.geoJSON工厂也接受GeoJSON对象的数组,但它似乎不是Leaflet.timeline插件的情况:它尝试直接读取data.features,就好像{{ 1}}是一个特征集合,而您将data作为特征集合数组传递。

在您的调试中,您甚至会明确地查看此数组的项目:data

最后,如果你写data[0].features,JS引擎希望forEach(feature)是一个函数(回调到feature),而不是函数参数,就像你写的那样例如forEach

因此,您可以非常简单地重构一下Rails后端,以发送一个未包含在数组中的Feature Collection对象。