Skip to content

Instantly share code, notes, and snippets.

@geog4046instructor
Last active January 1, 2018 22:12
Show Gist options
  • Save geog4046instructor/ba81b4f5bfbb2f2bef5b2ef1c60add31 to your computer and use it in GitHub Desktop.
Save geog4046instructor/ba81b4f5bfbb2f2bef5b2ef1c60add31 to your computer and use it in GitHub Desktop.
Leaflet - Click an HTML element to show or hide a layer
/* 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