gpt4 book ai didi

传单 v1.03 : Make CircleMarker draggable?

转载 作者:行者123 更新时间:2023-12-03 23:30:52 24 4
gpt4 key购买 nike

有没有传单大师有想法,制作 的最简单方法是什么?圆形标记 可在 中拖动传单 v1.0.3 ?

通过使用“可拖动”选项,可以很容易地为“标准”标记做到这一点。但是 CircleMarker 不存在这样的选项。我通过使用多个事件进行了尝试,但问题是,不是 move 标记而是底层 map 。

另一种可能性是使用“stopPropagation”-Function(但仅适用于 DOMEvents)。或者使用“removeEventParent”...如果 CircleMarker 的“父”是 map 及其事件?
关于文档,还有 DOMUtility/Draggable-class。这是我需要的吗?

<!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>

最佳答案

Leaflet v1.0+解决方案:

var marker = L.circleMarker([41.91847, -74.62634]).addTo(map)

// extract trackCursor as a function so this specific
// "mousemove" listener can be removed on "mouseup" versus
// all listeners if we were to use map.off("mousemove")
function trackCursor(evt) {
marker.setLatLng(evt.latlng)
}

marker.on("mousedown", function() {
map.dragging.disable()
map.on("mousemove", trackCursor)
})

map.on("mouseup", function() {
map.dragging.enable()
map.off("mousemove", trackCursor)
})
为了使这种行为更可重用,我们可以将其封装在一个函数中(JS ES6 语法):
function moveableMarker(map, marker) {
function trackCursor(evt) {
marker.setLatLng(evt.latlng)
}

marker.on("mousedown", () => {
map.dragging.disable()
map.on("mousemove", trackCursor)
})

marker.on("mouseup", () => {
map.dragging.enable()
map.off("mousemove", trackCursor)
})

return marker
}
然后,您可以使标记可拖动/move ,如下所示:
const moveable = moveableMarker(map, marker)
这些示例有助于构建上述解决方案:
  • Akshay Agrawal's JS Fiddle example
  • Jedidiah Hurt's Leaflet 1.0 draggable circle
  • 关于传单 v1.03 : Make CircleMarker draggable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43410600/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com