Skip to content
/ leaflet-map Public template

Leaflet map template to load local and remote files (GeoJSON, tileLayer, tileLayer.WMS) directly into legend toggle (L.control.layers) with simple jQuery call

License

Notifications You must be signed in to change notification settings

HandsOnDataViz/leaflet-map

Repository files navigation

leaflet-map

Leaflet map template to load local and remote files (GeoJSON, tileLayer, tileLayer.WMS) directly into legend toggle (L.control.layers) with simple jQuery calls.

Leaflet map demo

Demo with Leaflet 1.x

Why are my GeoJSON boundaries not dispalying?

If you open this repository on your local machine by double-clicking the index.html file, you will encounter a CORS (cross-origin request) issue that will prevent local GeoJSON files from loading via the file: protocol (see this issue for more details). To avoid this, you can try one of the following:

Why this template?

This template illustrates simple and flexible methods for non-experts to create maps that require loading GeoJSON files and tileLayers from local directories and remote servers. The template features Connecticut, where I design maps with students and community partners at Trinity College, Hartford CT, and also with collaborators at MAGIC, the Map and Geographic Information Center at UConn Libraries, on projects such as https://OnTheLine.trincoll.edu and https://HandsOnDataViz.org.

Learn more about Leaflet from these tutorials:

This template addresses my greatest challenge as novice coder: how to upload my own spatial data into Leaflet in the popular GeoJSON format, without getting lost in confusing jQuery functions. Leaflet's own intro tutorial skips over this important step. Other excellent tutorials show how to load GeoJSON data from inside a jQuery function, but in a way that does not easily allow you to place those layers in a toggle legend outside of that function. Finally, I discovered a simple, flexible solution posted by @iH8 on StackOverflow:

// Create the layercontrol and add it to the map
var controlLayers = L.control.layers().addTo(map);

// Loading 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 layercontrol
   controlLayers.addOverlay(geojsonLayer, 'My GeoJSON layer title');

});

See also working example by @iH8 on Plunker: http://plnkr.co/edit/tFVrrq?p=preview

Using this method, controlLayers is declared as a global variable near the top. When map layers are loaded in subsequent jQuery functions, they can be added directly to the map and/or to the legend toggle control. Overall, this approach seems more straightforward than other tutorials, yet I had not seen it described elsewhere. This template expands on the concept, inserts some sample layers and styling, and includes code comments for novices like me. Feedback and pull requests are welcome.

To Do

Credits

About

Leaflet map template to load local and remote files (GeoJSON, tileLayer, tileLayer.WMS) directly into legend toggle (L.control.layers) with simple jQuery call

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages