最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Finding center of Leaflet.js map after mouse move - Stack Overflow

programmeradmin4浏览0评论

I would like to detect after user move where the center of the map is. Currently I have this piece of code responsible for detecting what is the latitude and longitude of beginning of the mouse move:

var lat, lng;

    mymap.addEventListener('mousemove', function(ev) {
       lat = ev.latlng.lat;
       lng = ev.latlng.lng;
    });

    document.getElementById("mapid").addEventListener("click", function (event) {
        console.log(lat + ' - ' + lng);

        return false;
    });

Unfortunately it is not working as I'd imagine. I tried to find some event responsible for stopping of moving and beside drag end event which is only for markers, not just move I couldn't find such. Also the second problem would be then - how to detect the center of map.

Does anyone has any solution for this?

I would like to detect after user move where the center of the map is. Currently I have this piece of code responsible for detecting what is the latitude and longitude of beginning of the mouse move:

var lat, lng;

    mymap.addEventListener('mousemove', function(ev) {
       lat = ev.latlng.lat;
       lng = ev.latlng.lng;
    });

    document.getElementById("mapid").addEventListener("click", function (event) {
        console.log(lat + ' - ' + lng);

        return false;
    });

Unfortunately it is not working as I'd imagine. I tried to find some event responsible for stopping of moving and beside drag end event which is only for markers, not just move I couldn't find such. Also the second problem would be then - how to detect the center of map.

Does anyone has any solution for this?

Share Improve this question edited Nov 13, 2018 at 20:37 jww 102k103 gold badges441 silver badges941 bronze badges asked Jan 24, 2018 at 21:18 sebap123sebap123 2,6857 gold badges49 silver badges85 bronze badges 1
  • 1 The answer to your question is here: stackoverflow./a/68362453/1243247 – João Pimentel Ferreira Commented Jul 13, 2021 at 12:36
Add a ment  | 

1 Answer 1

Reset to default 15

If you want to keep track of the center of the map, you should be using mymap.getCenter() instead of ev.latlng.

发布评论

评论列表(0)

  1. 暂无评论