Reading a GeoJSON file into Rails/Leaflet

笑着哭i 提交于 2019-12-11 15:24:28

问题


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.

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"); seems to be an object and not usable. Or can this be parsed?

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.


回答1:


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

   } )

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



来源:https://stackoverflow.com/questions/49354442/reading-a-geojson-file-into-rails-leaflet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!