Google-maps – How to dynamically change the centre of a google maps without erasing markers

google mapsgoogle-maps-api-3

Initially, my map would be pointing to the user's current location. I am giving an option where in he can choose a city from an auto complete box, once he selects the new location from auto complete, I will get a new (lat,lng) coordinates for which I would like to focus my map to.

From my knowledge of Maps API, I am able to think of it as changing the centre(however, the centre is set only while loading the map). I want to achieve this focus to user selected location on google maps. Can some one help me out. Thanks in advance. Using Google Maps js api v3.

Best Answer

You can simply move the map with map.setCenter(LatLng). This method will not use any other markers and will not erase existing markers. You could also move the map via the panTo(LatLng) function. Both methods are also available after the map has been initialized.

Here is the documentation for these methods.