有没有一些了解Leaflet的专家知道,在Leaflet v1.0.3中使一个CircleMarker可拖动的最简单方法是什么?
对于“标准”标记,使用“draggable”选项很容易实现。但是CircleMarker没有这样的选项。我尝试使用多个事件来解决问题,但问题在于移动的不是标记,而是底层地图。
另一种可能性是使用“stopPropagation”函数(但仅适用于DOM事件)。或者使用“removeEventParent”... 如果CircleMarker的“parent”是地图及其事件呢?根据文档,还有DOMUtility/Draggable类。这是我需要的吗?
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Draggable Markers</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
<style>
body {padding: 0; margin: 0;}
html, body, #map {height: 100%;}
</style>
</head>
<body>
<div id="map"></div>
<script>
var layerOsm = new L.TileLayer('https://{s}.api.mapbox.com/v4/mapbox.outdoors/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoicHBldGUiLCJhIjoiY2lsdmE2ZmQ2MDA4OHZxbTZpcmx1emtqbSJ9.Two7SPSaIZysqgOTrrLkRg', {
subdomains: 'ab', maxZoom: 20, noWrap:true, attribution:'<a href="http://www.mapbox.com">Mapbox</a> | <a href="http://www.openstreetmap.org/copyright/">OpenStreetMap</a>' });
var map = new L.Map('map').addLayer(layerOsm).setView(new L.LatLng(47.8, 13.0), 14);
L.marker([47.8, 13.0], {draggable:true}).addTo(map);
var circle = L.circleMarker([47.81, 13.01], {radius:30}).addTo(map);
circle.on('mousedown', function () {
map.on('mousemove', function (e) {
circle.setLatLng(e.latlng);
});
});
map.on('mouseup', function(){
map.removeEventListener('mousemove');
})
</script>
</body>
</html>
L.circleMarker
,但可以替换为L.divIcon()
并使用 CSS 使其看起来像一个圆形,这样就可以将其设置为可拖动。请参见 https://gis.stackexchange.com/a/226814/。 - user2441511