Leaflet - 可拖动的标记和坐标以字段形式显示

2022-01-12 00:00:00 leaflet javascript

我必须做一个可拖动的标记,它的坐标应该显示在字段中.它将成为 PHP 中联系表单的一部分.我创建了一个可拖动的标记,请帮助我现在该怎么做.

I have to do a draggable marker and its coordinates should be displayed in fields. It will be a part of a contact form in PHP. I created a draggable marker, help me what to do now.

var marker = L.marker(new L.LatLng(53.471, 18.744), {
draggable: true
}).addTo(map);

http://jsfiddle.net/xTh5U/

这是 Google Maps API 中的示例,我需要在 Leaflet 中使用相同的示例.

Here is example in Google Maps API, I need the same in Leaflet.

推荐答案

你应该使用L.Marker的dragend事件,所以你知道拖动已经结束,然后使用L的getLatLng方法获取标记的坐标.标记.获取这些内容后,您可以将它们分配给文本输入的值.

You should use the dragend event of L.Marker, so you known dragging has ended, then get the coordinates of the marker by using the getLatLng method of L.Marker. When you've fetched those you can assign them to the values of your text inputs.

marker.on('dragend', function (e) {
    document.getElementById('latitude').value = marker.getLatLng().lat;
    document.getElementById('longitude').value = marker.getLatLng().lng;
});

Plunker 上的工作示例:http://plnkr.co/edit/iyMhaoAyllr2uNSOHhS9?p=preview

Working example on Plunker: http://plnkr.co/edit/iyMhaoAyllr2uNSOHhS9?p=preview

相关文章