Animated setView() in Leaflet

Viewed 13488

I have a search bar in my leaflet map and I would like the map to slowly zoom and pan to the marker when selected from the search bar. I can get it to zoom and pan but not slowly. My desired effect would be similar to when you type in a location in Google Earth and the view 'flys' from one location to the next. This is the code I have which zooms to my location but not slowly.

controlSearch = new L.Control.Search({layer:listOfMarkers, propertyName: 'IntersectionName', circleLocation:true,  position:'topleft'});
    map.addControl(controlSearch)

controlSearch.on('search_locationfound', function(e){
    map.setView(e.latlng,15, {animate:true, duration:10.0})
});

I'm using leaflet v0.7.7.

Thanks!

2 Answers

Another way

map.flyTo(e.latlng, map.getZoom(), {
      animate: true,
      duration: 0.5
    });
Related