Leaflet – Difference Between setView() and panTo() Methods

leaflet

Is there a difference between the actual zooming when using the setView and panTo methods in Leaflet?

map.setView([52, 13]);

map.panTo([52, 13]);

The zooming effect looks identical to me, or am I missing something? Are they the same unless you use the optional parameters?

Best Answer

There is no difference. In fact, have a look at Leaflet's source code for panTo:

panTo: function (center, options) { // (LatLng)
    return this.setView(center, this._zoom, {pan: options});
},

And setView:

setView: function (center, zoom, options) {
    zoom = zoom === undefined ? this._zoom : this._limitZoom(zoom);
    ...
},

As you can see, panTo() just calls setView() with the map's current zoom level, and setView() will also use the map's current zoom level if the second parameter is undefined.