如何从物体获取坐标?

时间:2019-06-10 20:03:35

标签: javascript leaflet

我是JS的初学者。 我有这个对象:

var lc = L.control.locate().addTo(map);
console.log(lc);

返回: https://ibb.co/r7T4wP7

我如何需要2个变量:long和lat具有该对象的值(纬度和经度)。如何获得它们?

2 个答案:

答案 0 :(得分:0)

lc ['_ event'] ['latitude']将带您进入纬度和

lc ['_ event'] ['longitude']将带您进入经度

为将来参考,建议您搜索: JavaScript对象变量

或尝试: https://www.w3schools.com/js/js_variables.asp

答案 1 :(得分:0)

如果您使用的是Leaflet.Locate plugin,则只需参考其文档mentions,即可使用Leaflet内置位置事件:

  

您可以利用本地Leaflet事件onlocationfoundonlocationerror来处理地理定位成功或产生错误的时间。您可以在Leaflet documentation中找到有关这些事件的更多信息。

链接的教程为您提供了一个代码片段,该代码片段如何使用位置找到事件获取坐标:

function onLocationFound(e) {
    var radius = e.accuracy; // Note: accuracy is already a radius

    L.marker(e.latlng).addTo(map)
        .bindPopup("You are within " + radius + " meters from this point").openPopup();

    L.circle(e.latlng, radius).addTo(map);
}

map.on('locationfound', onLocationFound);