153

The following code initializes a leaflet map. The initialize function centers the map based on user location. How do I change the center of the map to a new position after calling the initialize function?

function initialize() {
map = L.map('map');
L.tileLayer('http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png', {
    maxZoom: 18,
    attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>'
}).addTo(map);

map.locate({setView: true, maxZoom: 8});    
} 

6 Answers 6

227

For example:

map.panTo(new L.LatLng(40.737, -73.923));
3
  • 48
    map.flyTo([40.737, -73.923], 8) if you want to zoom and animate as well Commented Dec 10, 2018 at 15:51
  • 9
    In my case however, panTo(), flyTo(), setView() - all of them take me to the top left of the map, and not the center. Commented Apr 27, 2019 at 12:42
  • 1
    @MrigankPawagi use setTimeout(function () {map.invalidateSize(true)}, 100); Commented Mar 18, 2022 at 12:59
160

You can also use:

map.setView(new L.LatLng(40.737, -73.923), 8);

It just depends on what behavior you want. map.panTo() will pan to the location with zoom/pan animation, while map.setView() immediately set the new view to the desired location/zoom level.

4
  • 4
    I like this one because you also get the zoom level, which is very useful. Commented Oct 16, 2014 at 18:12
  • 5
    Also you can pass the coordinates as an array: map.setView([40.737, -73.923], 8) or an object map.setView({lat:40.737, lng:-73.923}, 8) Commented Feb 11, 2016 at 17:48
  • 7
    panTo was not showing animation, I use map.setView(latlng, map.getZoom(), { animation: true }); Commented Jan 24, 2017 at 15:49
  • animation still not working with setView
    – chovy
    Commented May 1, 2022 at 14:03
15

Use map.panTo(); does not do anything if the point is in the current view. Use map.setView() instead.

I had a polyline and I had to center map to a new point in polyline at every second. Check the code : GOOD: https://jsfiddle.net/nstudor/xcmdwfjk/

mymap.setView(point, 11, { animation: true });        

BAD: https://jsfiddle.net/nstudor/Lgahv905/

mymap.panTo(point);
mymap.setZoom(11);
1
  • Doesnt' animate for me.
    – chovy
    Commented May 1, 2022 at 14:02
7

You could also use:

var latLon = L.latLng(40.737, -73.923);
var bounds = latLon.toBounds(500); // 500 = metres
map.panTo(latLon).fitBounds(bounds);

This will set the view level to fit the bounds in the map leaflet.

2

In the case of map-centering problem despite using panTo(),flyTo() or setView() try to adjust the map with map.invalidateSize():

setTimeout(function () {
    map.invalidateSize(true);
}, 100);

jQuery sample:

$(document).ready(function () {
    mymap.invalidateSize();
});
0

I was looking for a way to change the bounds but without the animation. This worked for me:

var bounds = L.latLng(40.737, -73.923).toBounds();
map.fitBounds(bounds, {animation: false});
1
  • I have many markers in the map, my use case is to select a marker and zoom it and I want to keep it on the center of the map when zoomend
    – Don2
    Commented Dec 16, 2023 at 2:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.