You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
5.9 KiB
145 lines
5.9 KiB
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<title>Leaflet.draw popup event example</title> |
|
|
|
<script src="libs/leaflet-src.js"></script> |
|
<link rel="stylesheet" href="libs/leaflet.css" /> |
|
|
|
<script src="../../src/Leaflet.draw.js"></script> |
|
<script src="../../src/Leaflet.Draw.Event.js"></script> |
|
<link rel="stylesheet" href="../../src/leaflet.draw.css" /> |
|
|
|
<script src="../../src/Toolbar.js"></script> |
|
<script src="../../src/Tooltip.js"></script> |
|
|
|
<script src="../../src/ext/GeometryUtil.js"></script> |
|
<script src="../../src/ext/LatLngUtil.js"></script> |
|
<script src="../../src/ext/LineUtil.Intersect.js"></script> |
|
<script src="../../src/ext/Polygon.Intersect.js"></script> |
|
<script src="../../src/ext/Polyline.Intersect.js"></script> |
|
<script src="../../src/ext/TouchEvents.js"></script> |
|
|
|
<script src="../../src/draw/DrawToolbar.js"></script> |
|
<script src="../../src/draw/handler/Draw.Feature.js"></script> |
|
<script src="../../src/draw/handler/Draw.SimpleShape.js"></script> |
|
<script src="../../src/draw/handler/Draw.Polyline.js"></script> |
|
<script src="../../src/draw/handler/Draw.Marker.js"></script> |
|
<script src="../../src/draw/handler/Draw.CircleMarker.js"></script> |
|
<script src="../../src/draw/handler/Draw.Circle.js"></script> |
|
<script src="../../src/draw/handler/Draw.Polygon.js"></script> |
|
<script src="../../src/draw/handler/Draw.Rectangle.js"></script> |
|
|
|
|
|
<script src="../../src/edit/EditToolbar.js"></script> |
|
<script src="../../src/edit/handler/EditToolbar.Edit.js"></script> |
|
<script src="../../src/edit/handler/EditToolbar.Delete.js"></script> |
|
|
|
<script src="../../src/Control.Draw.js"></script> |
|
|
|
<script src="../../src/edit/handler/Edit.Poly.js"></script> |
|
<script src="../../src/edit/handler/Edit.SimpleShape.js"></script> |
|
<script src="../../src/edit/handler/Edit.Marker.js"></script> |
|
<script src="../../src/edit/handler/Edit.CircleMarker.js"></script> |
|
<script src="../../src/edit/handler/Edit.Circle.js"></script> |
|
<script src="../../src/edit/handler/Edit.Rectangle.js"></script> |
|
|
|
</head> |
|
<body> |
|
<div id="map" style="width: 800px; height: 600px; border: 1px solid #ccc"></div> |
|
|
|
<script> |
|
var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', |
|
osmAttrib = '© <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors', |
|
osm = L.tileLayer(osmUrl, {maxZoom: 18, attribution: osmAttrib}), |
|
map = new L.Map('map', {center: new L.LatLng(29.9792, 31.1344), zoom: 15}), |
|
drawnItems = L.featureGroup().addTo(map); |
|
|
|
L.control.layers({ |
|
"osm": osm.addTo(map), |
|
"google": L.tileLayer('http://www.google.cn/maps/vt?lyrs=s@189&gl=cn&x={x}&y={y}&z={z}', { |
|
attribution: 'google' |
|
}) |
|
}, {'drawlayer':drawnItems}, { position: 'topright', collapsed: false }).addTo(map); |
|
|
|
map.addControl(new L.Control.Draw({ |
|
edit: { |
|
featureGroup: drawnItems, |
|
poly : { |
|
allowIntersection : false |
|
} |
|
}, |
|
draw: { |
|
polygon : { |
|
allowIntersection: false, |
|
showArea:true |
|
} |
|
} |
|
})); |
|
|
|
// Truncate value based on number of decimals |
|
var _round = function(num, len) { |
|
return Math.round(num*(Math.pow(10, len)))/(Math.pow(10, len)); |
|
}; |
|
// Helper method to format LatLng object (x.xxxxxx, y.yyyyyy) |
|
var strLatLng = function(latlng) { |
|
return "("+_round(latlng.lat, 6)+", "+_round(latlng.lng, 6)+")"; |
|
}; |
|
|
|
// Generate popup content based on layer type |
|
// - Returns HTML string, or null if unknown object |
|
var getPopupContent = function(layer) { |
|
// Marker - add lat/long |
|
if (layer instanceof L.Marker || layer instanceof L.CircleMarker) { |
|
return strLatLng(layer.getLatLng()); |
|
// Circle - lat/long, radius |
|
} else if (layer instanceof L.Circle) { |
|
var center = layer.getLatLng(), |
|
radius = layer.getRadius(); |
|
return "Center: "+strLatLng(center)+"<br />" |
|
+"Radius: "+_round(radius, 2)+" m"; |
|
// Rectangle/Polygon - area |
|
} else if (layer instanceof L.Polygon) { |
|
var latlngs = layer._defaultShape ? layer._defaultShape() : layer.getLatLngs(), |
|
area = L.GeometryUtil.geodesicArea(latlngs); |
|
return "Area: "+L.GeometryUtil.readableArea(area, true); |
|
// Polyline - distance |
|
} else if (layer instanceof L.Polyline) { |
|
var latlngs = layer._defaultShape ? layer._defaultShape() : layer.getLatLngs(), |
|
distance = 0; |
|
if (latlngs.length < 2) { |
|
return "Distance: N/A"; |
|
} else { |
|
for (var i = 0; i < latlngs.length-1; i++) { |
|
distance += latlngs[i].distanceTo(latlngs[i+1]); |
|
} |
|
return "Distance: "+_round(distance, 2)+" m"; |
|
} |
|
} |
|
return null; |
|
}; |
|
|
|
// Object created - bind popup to layer, add to feature group |
|
map.on(L.Draw.Event.CREATED, function(event) { |
|
var layer = event.layer; |
|
var content = getPopupContent(layer); |
|
if (content !== null) { |
|
layer.bindPopup(content); |
|
} |
|
drawnItems.addLayer(layer); |
|
}); |
|
|
|
// Object(s) edited - update popups |
|
map.on(L.Draw.Event.EDITED, function(event) { |
|
var layers = event.layers, |
|
content = null; |
|
layers.eachLayer(function(layer) { |
|
content = getPopupContent(layer); |
|
if (content !== null) { |
|
layer.setPopupContent(content); |
|
} |
|
}); |
|
}); |
|
</script> |
|
</body> |
|
</html>
|
|
|