将GeoJSON文件读入Rails/Leaflet [英] Reading a GeoJSON file into Rails/Leaflet

查看:88
本文介绍了将GeoJSON文件读入Rails/Leaflet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用Rails jBuilder创建了GeoJSON文件,并想将其读入Leaflt.如果我像L.mapbox.featureLayer().loadURL('overview/overview_data.geojson').addTo(map)这样直接使用它也可以,但是我想使用Leaflet.timeline并与GeoJSON一起使用.

I've created GeoJSON file with Rails jBuilder and want to read it in to Leaflt. It's OK if I use it directly like so L.mapbox.featureLayer().loadURL('overview/overview_data.geojson').addTo(map) but I want to use Leaflet.timeline and work with the GeoJSON.

如果我将GeoJSON这样放置在脚本中

If I put the GeoJSON in the script like so

let 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 became part of S. Toluca St. (26). and second block south gone until at least 1908."},"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 became part of E. 40th Place until at least 1928."},..

脚本的其余部分工作正常,但是我需要使用动态生成的数据(我做了上面的测试).

the rest of the script works fine, but I need to use dynamically generated data (I did the above for testing).

let data = new L.GeoJSON("overview/overview_data.json");似乎是一个对象,无法使用.还是可以解析?

let data = new L.GeoJSON("overview/overview_data.json"); seems to be an object and not usable. Or can this be parsed?

另一种选择是,如果jBuilder可以在GeoJSON周围添加一个回调包装,而我可以使用它.

The other alternative would be if jBuilder could add a callback wrapper around the GeoJSON and I could work with that.

我知道已经解决了这个问题,但是我不知道该怎么做.

I know this has been addressed, but I can't figure out how to do it.

推荐答案

这一次有效.发布问题使我得到了答案.

Worked this time. Posting a question led me to an answer.

  $.getJSON("overview/overview_data.json", function (data) {

  <all the stuff I needed to to

   } )

JavaScript的奥秘之一.可以像我正在尝试的那样做吗.

One of the mysteries of JavaScript. Can it be done something like I was trying.

这篇关于将GeoJSON文件读入Rails/Leaflet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆