有没有可能通过传单中带有标记的定位控件来获得经纬度?

Is there any possible way to get lat & long through locate control with marker in leaflet?

我已将控件定位 css & js 准备就绪,但我无法弄清楚。我计划在按下定位按钮时实现它,它将使用标记将我定位到当前方向,并在我的网络表单中自动填充纬度和经度(如附件)。

lat & long form

下面是我现有的搜索控件代码。找到搜索地址时,功能自动填充和标记 lat/long。

<script type="text/javascript">
var mymap = L.map(\'dvMap\').setView(['. $pin_lat . ', ' . $pin_long . '],13);
L.tileLayer(\'https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}{r}.png\',{maxZoom: 18,attribution: \'\'}).addTo(mymap);
var markerz = L.marker([' . $pin_lat . ', ' . $pin_long . '],{draggable:
true}).addTo(mymap);
var searchControl   =   new L.Control.Search({url:
\'//nominatim.openstreetmap.org/search?format=json&q={s}\',jsonpParam:
\'json_callback\',
propertyName: \'display_name\',propertyLoc:
[\'lat\',\'lon\'],marker: markerz,autoCollapse: true,autoType:
true,minLength: 2,});
searchControl.on(\'search:locationfound\', function(obj) {      
    var lt  =   obj.latlng + \'\';
    var res = lt.split("LatLng(" );
    res = res[1].split( ")" );
    res = res[0].split( ","
);
document.getElementById(\'map_lat\').value = res[0];
document.getElementById(\'map_long\').value = res[1];
});

mymap.addControl( searchControl );
markerz.on(\'dragend\', function (e) {
        document.getElementById(\'map_lat\').value =
        markerz.getLatLng().lat;
        document.getElementById(\'map_long\').value =
        markerz.getLatLng().lng;
});
</script>

我想用locate控件做一个类似的功能

根据docs,可以使用原生Leaflet事件locationfound:

map.on('locationfound',(e)=>{
    console.log(e);
    document.getElementById(\'map_lat\').value = e.latlng.lat;
    document.getElementById(\'map_long\').value = e.latlng.lng;
    // if you want, you can trim the number
    document.getElementById(\'map_long\').value = e.latlng.lng.toFixed(6);
})

Here jsfiddle 上的示例。位置搜索有效。