【问题标题】:Get latlng of the map when panned or zoomed平移或缩放时获取地图的纬度
【发布时间】:2018-05-17 21:16:15
【问题描述】:

我试图在拖动、平移或缩放时获取地图中心的 lat lng。我想在拖动、平移或缩放地图时立即更新地图中心的 lat lng。我正在使用以下代码

map.on('focus', function(e) {
   console.log("Lat, Lon : " + e.latlng.lat + ", " + e.latlng.lng)
});

我收到错误 Cannot read property 'lat' of undefined。但是当我使用click而不是focus时,代码可以工作。

这可以通过谷歌地图轻松完成

map.addListener('get_center',function(){
  console.log("Lat, Lng:"+ map.center.lat()","+ map.center.lng())
})

有谁知道传单中的正确方法

【问题讨论】:

    标签: javascript leaflet


    【解决方案1】:

    您需要随时使用getCenter() 方法获取lat 和lng。根据需要使用缩放和拖动事件。 Refer the documentation。确保您使用 getCenter() 如下

    map.on('dragend', function(e) {
        console.log('Lat: ' +  map.getCenter().lat + ' Lng: ' + map.getCenter().lng);
    });
    

    【讨论】:

    • moveend 也很有用 - 只要地图中心通过拖动或编程方式移动,它就会触发
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    相关资源
    最近更新 更多