【问题标题】:Leaflet.timeline geojson parsing errorLeaflet.timeline geojson 解析错误
【发布时间】:2018-03-12 02:13:13
【问题描述】:

尝试实现 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

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

最后是控制台中的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 进行了一些重组,因为这里提到的问题已经消失了。

【问题讨论】:

    标签: javascript leaflet geojson


    【解决方案1】:

    很可能您不应该将特征集合包装在数组中。

    虽然 Leaflet 的 L.geoJSON 工厂确实也接受 GeoJSON 对象数组,但 Leaflet.timeline 插件似乎并非如此:它尝试直接读取 data.features,就好像 data 是一个单一的特征集合,而您将 data 作为特征集合数组传入。

    在您的调试中,您甚至可以明确查看此数组的一项:data[0].features

    最后,如果你写forEach(feature),JS 引擎期望feature 是一个函数(forEach 的回调),而不是一个函数参数,如果你写了forEach(feature =&gt; {console.log(feature);}

    因此,您可以非常简单地重构一点您的 Rails 后端以发送单个功能集合对象,而不是包装在数组中。

    【讨论】:

    • 我想我明白你在说什么。但是这些示例使用了更多的单一功能,但它们都是 jsonp,一个是来自earthquake.usgs.gov/earthquakes/feed/v1.0/summary/… 的提要并开始:
    • 你听起来混淆了Feature CollectionFeature。您提到的端点返回一个带有 single Feature Collection 的 JSONP,其中可能包含 plenty Features。跨度>
    • 但是在仔细查看示例时会进行一些预处理。我得再调查一下。由于存储库与发布的示例不匹配并且将脚本拼凑在一起,这一事实使情况变得复杂。感谢您的评论。我会看看两个cmets。我会错过你在上一条评论中的区别。
    • 我认为我的数据与示例类似。但我会看更多,可能要到明天。 @ghybs 谢谢
    猜你喜欢
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 2020-01-04
    • 2016-08-25
    • 2017-08-02
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多