1 回答

TA貢獻1773條經驗 獲得超3個贊
使用Leaflet.Path.Drag插件。
包括腳本
<script src="https://cdn.jsdelivr.net/npm/[email protected]/src/Path.Drag.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>Quick Start - Leaflet</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" crossorigin="" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew==" crossorigin=""></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/src/Path.Drag.min.js"></script>
</head>
<body>
<div id="map" style="width: 600px; height: 400px;"></div>
<script>
var map = L.map('map').setView([3.1377736432253345, 101.56585693359375], 10);
var osm = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);
var poly1 = [
[
[3.1377736432253345, 101.56585693359375],
[2.929326028392636, 101.6619873046875],
[3.03629758922721, 101.89544677734375],
[3.247466393872138, 101.8048095703125]
]
]
var polygon = new L.Polygon([poly1], {
draggable: true,
color: '#810541',
fillColor: '#D462FF',
fillOpacity: 0.5,
}).addTo(map);
polygon.on('dragend', function(e) {
console.log(e.target._latlngs[0][0]);
});
</script>
</body>
</html>
添加回答
舉報