[GIS] Adding drop-down menu to leaflet map to update map/tiles to new location

javascriptleaflet

What I want to do is create an interactive choropleth map that has a dropdown menu of various countries so that the map updates when you click on that country.

As an example, here's an interactive choropleth of Ethiopia that I have, along with the code in the index.html file:

http://laurenoldham1202.github.io/magic-ethiopia/
https://github.com/laurenoldham1202/magic-ethiopia

I want to recreate this map so that it has a drop-down menu of about 2 dozen other countries with the same content. The map will act the same way as it does with just Ethiopia, but the user will have options to choose which country they want to view. When they click on that country, the map will update to show that country.

Is there a way to do this inside of Leaflet?

Best Answer

I see no special difficulty in implementing what you describe.

The drop-down menu can be either external or styled as a map control, like you have done for your legends and slider. Then you can react to "change" event to detect selection of a new country.

Panning to the new country can be easily done through map.fitBounds() and having your shapes gathered in a Feature Group so that you can easily retrieve its bounds.

Then you can show only the shapes of the chosen country and hide all the other ones, through simple map.addLayer() and map.removeLayer().

You might also need to swap your legends as well, in case your data is not normalized across all countries.


EDIT:

By the way, as for the drop-down menu and panning the map to the selected country, you might be interested in this plugin (or look at how it is done inside): Leaflet.CountrySelect (demo)