How can I change the location center of a map using Leaflet API?

前端 未结 4 1774
礼貌的吻别
礼貌的吻别 2021-02-04 21:11

My map (Mapbox) takes up the whole background of the site so the center is in the middle of the site. But the focus of the map for the user is on the right side because I have

4条回答
  •  春和景丽
    2021-02-04 22:04

    You can use a combo of panBy() and getSize() to offset the map the width of your overlay.

    Here's a snippet that should get you a started:

    // Calculate the offset
    var offset = map.getSize().x*0.15;
    // Then move the map
    map.panBy(new L.Point(-offset, 0), {animate: false});
    

    In my example, my overlay is 33% of the width of the map. So I grab the size of the map area, then multiple by 0.15 (this was based on some experimenting to see what the best offset amount was) and use panBy() to offset the map center.

    Here's a full example.

提交回复
热议问题