問題描述
我必須做一個可拖動的標記,它的坐標應該顯示在字段中.它將成為 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
這篇關于Leaflet - 可拖動的標記和坐標以字段形式顯示的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!