Learning Leaflet. Noticed examples add a var name to the GeoJson file. Necessary?

103 Views Asked by At

I'm learning Leaflet, JS, etc. Am testing using code examples from the Leaflet site, modified for my use. In all the example I've looked at, the JSON file has a var added in front of the initial bracket ([). Is this necessary to work with a JSON file? It sure would be nice to have the JSON files work as they are generated. ArcMap Desktop, for example, has a tool called Feature-to-JSON and the output does not have a "var = name" as the first data. Can I avoid the step of adding the var? If so, I'd appreciate code examples.

1

There are 1 best solutions below

5
On

You definitely don't have to save your JSON files as JavaScript files. It is used in the examples just for simplicity. Normally, you have your JSON file on your server or accessible with some REST API. This file can be asynchronously downloaded and used in your map like this:

myHttpService.get(myFileUrl).then(function(jsonData) {
    L.geoJson(jsonData).addTo(map);
});