传单geojson更新可拖动状态或标记 [英] Leaflet geojson update draggable state or marker
问题描述
我想知道是否可以在传单的GEOJSON层中更新标记的可拖动事件,我知道我可以通过添加以下内容来实现:
I would like to know if it's possible to update the draggable event of a marker inside a GEOJSON layer in leaflet, I know I can do this by adding:
layer.options.draggable = True
在onEachFeature函数中,我试图实现的功能是,在元素单击上更新可拖动选项,例如:
Inside the onEachFeature function, what I'm trying to achieve is, to update the draggable options on an element click, something like:
$(document).on('click','#someButton',function(){
layer.options.draggable = True; //Only one specific marker
});
这样,我想禁用所有带有可拖动选项的标记,然后单击按钮,仅对一个特定标记启用可拖动选项.是否可以使用geojson层实现此目的?我在featureGroup中也有这个geojson图层,希望大家能帮帮我.谢谢
This way I would like to have all my marker with draggable options disabled, then on a button click, enable the draggable option, only for one specific marker. Is it possible to achieve this using geojson layer? I also have this geojson layer inside a featureGroup, hope you guys can help me out. Thanks
推荐答案
You can dynamically enable / disable the draggability of a Leaflet Marker by simply removing it from the map, setting (resetting) its draggable
option, then re-adding it to the map:
var map = L.map('map').setView([48.86, 2.35], 11);
var marker = L.marker([48.86, 2.35]).addTo(map);
document.getElementById('dragonoff').addEventListener('click', function(event) {
event.preventDefault();
// Toggle Marker draggability.
marker.remove();
marker.options.draggable = !marker.options.draggable;
marker.addTo(map);
alert(marker.options.draggable ? 'Marker is now draggable' : 'Marker is no longer draggable');
});
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css">
<script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet-src.js"></script>
<div id="map" style="height: 150px"></div>
<button id="dragonoff">Enable / Disable dragging</button>
是否通过GeoJSON Leaflet图层组创建标记无关紧要.
Whether you create your Marker through a GeoJSON Leaflet layer group or not is irrelevant.
这篇关于传单geojson更新可拖动状态或标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!