Last active
September 3, 2016 06:08
-
-
Save rowanwins/14f7ffdbd837ff491631 to your computer and use it in GitHub Desktop.
Limiting Leaflet.draw
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> | |
<html> | |
<head> | |
<title>Leaflet.draw drawing and editing tools</title> | |
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" /> | |
<link rel="stylesheet" href="js/leaflet.draw.css" /> | |
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script> | |
<script src="js/leaflet.draw.js"></script> | |
<style> | |
html,body{height:100%;margin:0px;} | |
</style> | |
</head> | |
<body> | |
<div id="map" style="height:100%; width:100%"></div> | |
<script> | |
map = L.map('map').setView([51.505, -0.09], 13); | |
L.tileLayer('https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', { | |
maxZoom: 18, | |
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://mapbox.com">Mapbox</a>', | |
id: 'examples.map-i875mjb7' | |
}).addTo(map); | |
var drawnItems = new L.FeatureGroup(); | |
map.addLayer(drawnItems); | |
var drawControlFull = new L.Control.Draw({ | |
edit: { | |
featureGroup: drawnItems | |
}, | |
draw: { | |
polyline: false | |
} | |
}); | |
var drawControlEditOnly = new L.Control.Draw({ | |
edit: { | |
featureGroup: drawnItems | |
}, | |
draw: false | |
}); | |
map.addControl(drawControlFull); | |
map.on("draw:created", function (e) { | |
var layer = e.layer; | |
layer.addTo(drawnItems); | |
drawControlFull.removeFrom(map); | |
drawControlEditOnly.addTo(map) | |
}); | |
map.on("draw:deleted", function(e) { | |
check = Object.keys(drawnItems._layers).length; | |
console.log(check); | |
if (check === 0){ | |
drawControlEditOnly.removeFrom(map); | |
drawControlFull.addTo(map); | |
}; | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See write up here
http://rowanwinsemius.id.au/blog/limiting-leaflet-draw-to-drawing-a-single-feature/