Created
January 22, 2014 18:44
-
-
Save mollietaylor/8564724 to your computer and use it in GitHub Desktop.
Add and Remove Leaflet Circle on Click
This file contains 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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<html> | |
<head> | |
<title>Click Circle</title> | |
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css" /> | |
<!--[if lte IE 8]> | |
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.ie.css" /> | |
<![endif]--> | |
<script src="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.js"></script> | |
<style type="text/css"> | |
#map { | |
height: 500px; | |
} | |
</style> | |
</head> | |
<body> | |
<h1></h1> | |
<div id="map"></div> | |
<script type="text/javascript"> | |
var map = L.map('map', { | |
center: [33.8, -84.4], | |
zoom: 11 | |
}); | |
L.tileLayer('http://tile.cloudmade.com/YOUR_API_KEY/29889/256/{z}/{x}/{y}.png', { | |
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>', | |
maxZoom: 18 | |
}).addTo(map); | |
// click circle: | |
var clickCircle; | |
function onMapClick(e) { | |
if (clickCircle != undefined) { | |
map.removeLayer(clickCircle); | |
}; | |
clickCircle = L.circle(e.latlng, 1609 * 3, { | |
color: '#f07300', | |
fillOpacity: 0, | |
opacity: 0.5 | |
}).addTo(map); | |
} | |
map.on('click', onMapClick); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Shanyuk : you could first put your circles (or whatever) into one or more LayerGroup, and remove the corresponding LayerGroup from your map when a click occurs on one of its circle. See these Layer Groups examples