Last active
January 1, 2018 22:12
-
-
Save geog4046instructor/ba81b4f5bfbb2f2bef5b2ef1c60add31 to your computer and use it in GitHub Desktop.
Leaflet - Click an HTML element to show or hide a layer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This example will listen for a click on an HTML element, in this case a button with id="my-btn", | |
* and show/hide a specified layer, in this case myLayer. | |
* | |
*/ | |
// wait for the user to click an element with id="my-btn", then run this code | |
jQuery( '#my-btn' ).click( function(){ | |
if ( map.hasLayer( myLayer ) ){ // if the specified layer is visible on the map, it needs to be removed | |
map.removeLayer( myLayer ) | |
return | |
} | |
map.addLayer( myLayer ) // if it is not on the map, it needs to be added | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment