If you're looking for a helpful tutorial on how to use L.GeoJSON
, Leaflet's GeoJSON layer, look no further! You can find a great guide here: http://leafletjs.com/examples/geojson.html. Additionally, for more information on L.GeoJSON
, check out the reference link here: http://leafletjs.com/reference.html#geojson. I see you've also discovered the tutorial for L.control.layers
; here is the reference for that as well: http://leafletjs.com/reference.html#control-layers
It's actually quite straightforward to implement. Just create a layer control, load a GeoJSON file into your script using any XHR library you prefer, utilize the retrieved data to define a L.GeoJSON
layer, and then add it to the layer control. Here's an example in code:
// Create the layer control and add it to the map
var controlLayers = L.control.layers().addTo(map);
// Load a GeoJSON file (using jQuery's $.getJSON)
$.getJSON('/my-folder/my-file.json', function (data) {
// Use the data to create a GeoJSON layer and add it to the map
var geojsonLayer = L.geoJson(data).addTo(map);
// Add the GeoJSON layer to the layer control
controlLayers.addOverlay(geojsonLayer, 'My GeoJSON layer title');
});
For a live demonstration, check out this Plunker example: http://plnkr.co/edit/tFVrrq?p=preview