2

I'm trying to read a geojson file into leaflet. Everything works out when I copy the text from the geojson file and paste it into

var data = { }

So I think the file should be ok for leaflet. But I rather want to be able to read the actual file. Right now I keep all files locally on my hard drive. I loaded Ajax and tried to read the file by

var data = L.GeoJSON.AJAX("data.json")

If I open the HTML the map loads, but not the point markers of the geojson file. When I use the Firefox Console I get:

TypeError: this.callInitHooks is not a function

What am I doing wrong?

  • It should be L.geoJSON or new L.GeoJSON. – TomazicM Mar 8 '18 at 17:56
  • Thanks for your answer. But it didn't solve my problem. If I use L.geoJSON I get the same error, with new L.geoJSON I get Error: Invalid GeoJSON object., which doesn't make sense, since it works when I use the same data and var data = L.GeoJSON.AJAX("data.json") – andieich Mar 8 '18 at 18:27
  • 1
    Sorry, I was not mindful enough when answering. If you are using leaflet-ajax plugin, it should definitely be var geojsonLayer = new L.GeoJSON.AJAX("geojson.json"); or var geojsonLayer = L.geoJson.ajax("data.json"); . See github.com/calvinmetcalf/leaflet-ajax – TomazicM Mar 8 '18 at 19:15
  • @TomazicM: you should turn your comment into a proper answer. – ghybs Mar 9 '18 at 9:33
  • with both I get Error: Invalid GeoJSON object. – andieich Mar 9 '18 at 13:37
1

I found the answer myself after reading this

Apparently, just reading a GeoJSON file is not working, since you need the "response" of it:

L.geoJSON(data.responseJSON)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.