leaflet实现图形可编辑
时间: 2024-06-08 08:09:06 浏览: 156
Leaflet是一个流行的JavaScript库,用于创建交互式地图。它提供了一些插件,例如Leaflet.draw,可以用于在地图上绘制和编辑各种形状,例如多边形、线和点。
以下是实现图形可编辑的基本步骤:
1. 引入Leaflet库和Leaflet.draw插件。
```html
<!-- Leaflet stylesheet -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" integrity="sha384-5cXvS2+9sG9Lm2p0Ue33xZ2iKd0lW2Ua/ExL6zHsWvZT3X7HxuOJ1nZC4iIWFVxZ" crossorigin="anonymous">
<!-- Leaflet script -->
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js" integrity="sha384-TnQrZzAu6ZgK4JfJ0ZgjLkKHWlb8aO3oIQ7a/2N5Zz95tiQeE7i+4Zl7yL2zW3C6" crossorigin="anonymous"></script>
<!-- Leaflet.draw stylesheet -->
<link rel="stylesheet" href="https://unpkg.com/leaflet-draw@1.0.4/dist/leaflet.draw.css"/>
<!-- Leaflet.draw script -->
<script src="https://unpkg.com/leaflet-draw@1.0.4/dist/leaflet.draw.js"></script>
```
2. 创建地图容器。
```html
<div id="map"></div>
```
```css
#map {
height: 500px;
}
```
```javascript
var map = L.map('map').setView([51.505, -0.09], 13);
```
3. 添加图层和控件。
```javascript
// 添加OpenStreetMap图层
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
'<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
}).addTo(map);
// 添加绘制控件
var drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
var drawControl = new L.Control.Draw({
edit: {
featureGroup: drawnItems
},
draw: {
polyline: false,
circle: false,
marker: false
}
});
map.addControl(drawControl);
// 监听图形编辑事件
map.on('draw:edited', function (e) {
var layers = e.layers;
layers.eachLayer(function (layer) {
// 处理编辑后的图形数据
console.log(layer.toGeoJSON());
});
});
// 监听图形创建事件
map.on('draw:created', function (e) {
var layer = e.layer;
// 添加新的图形到图层
drawnItems.addLayer(layer);
});
```
4. 运行示例代码,即可在地图上绘制和编辑图形。
```javascript
// 绘制多边形
var latlngs = [[51.509, -0.08], [51.503, -0.06], [51.51, -0.047]];
var polygon = L.polygon(latlngs).addTo(map);
// 编辑图形
polygon.enableEdit();
```
完整的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Leaflet Draw Example</title>
<!-- Leaflet stylesheet -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" integrity="sha384-5cXvS2+9sG9Lm2p0Ue33xZ2iKd0lW2Ua/ExL6zHsWvZT3X7HxuOJ1nZC4iIWFVxZ" crossorigin="anonymous">
<!-- Leaflet script -->
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js" integrity="sha384-TnQrZzAu6ZgK4JfJ0ZgjLkKHWlb8aO3oIQ7a/2N5Zz95tiQeE7i+4Zl7yL2zW3C6" crossorigin="anonymous"></script>
<!-- Leaflet.draw stylesheet -->
<link rel="stylesheet" href="https://unpkg.com/leaflet-draw@1.0.4/dist/leaflet.draw.css"/>
<!-- Leaflet.draw script -->
<script src="https://unpkg.com/leaflet-draw@1.0.4/dist/leaflet.draw.js"></script>
<style>
#map {
height: 500px;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = L.map('map').setView([51.505, -0.09], 13);
// 添加OpenStreetMap图层
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
'<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
}).addTo(map);
// 添加绘制控件
var drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
var drawControl = new L.Control.Draw({
edit: {
featureGroup: drawnItems
},
draw: {
polyline: false,
circle: false,
marker: false
}
});
map.addControl(drawControl);
// 监听图形编辑事件
map.on('draw:edited', function (e) {
var layers = e.layers;
layers.eachLayer(function (layer) {
// 处理编辑后的图形数据
console.log(layer.toGeoJSON());
});
});
// 监听图形创建事件
map.on('draw:created', function (e) {
var layer = e.layer;
// 添加新的图形到图层
drawnItems.addLayer(layer);
});
// 绘制多边形
var latlngs = [[51.509, -0.08], [51.503, -0.06], [51.51, -0.047]];
var polygon = L.polygon(latlngs).addTo(map);
// 编辑图形
polygon.enableEdit();
</script>
</body>
</html>
```
阅读全文