1

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.

Greg
  • 2,359
  • 5
  • 22
  • 35

1 Answers1

0

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.

Greg
  • 2,359
  • 5
  • 22
  • 35
  • Good job in solving your issue by yourself! It is not mystery, it is [asyncronicity](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). With your mapbox load url method, you probably have a loaded or ready event that you could attach a listener to, and pass your layer to Leaflet timeline. If the latter needs raw GeoJSON data, then using directly jQuery is more appropriate indeed. – ghybs Mar 19 '18 at 03:33
  • Makes some sense. Thanks – Greg Mar 19 '18 at 04:49