Search code examples
javascriptangularleafletopenstreetmap

Changing the Center of a leaflet open street map


I'm initializing a map using leaflet(openstreetmaps) in angular and setting a center that the user sees when opening the site. How can i change the center so the map rotates to the new coordinates on click of e.g. a button? Setting a new center doesn't appear to do anything, neither do i want to reload the site itself. Thanks!

private initMap(): void {

    this.map = L.map('map', {
      center: [48.1841234, 11.5877796],
      zoom: 16
    });}

Solution

  • you can use map.flyTo([latlng]) or map.panTo([latlng])

    https://leafletjs.com/reference-1.6.0.html#map-panto