Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save and restore position of map in google map v3

I used Google API version 2 in my previous assignment. There I had used map.savePosition() to save the current position of map and map.returnToSavedPosition() to restore to the saved position. I have searched for the equivalent in api version 3 documentation but could not find relevant results. And if I use map.savePosition() now with api-3, javascript error tells "map.savePosition is not a function".

Can someone please tell me what are the ways to save and restore the position of Google Map in API 3 ?

like image 506
tusar Avatar asked Feb 25 '23 00:02

tusar


2 Answers

As said above, there's no similar function in V3. It's really easy to implement yourself.

Here's one way:

var previousPosition;
function savePosition(map) {
  previousPosition = map.getCenter();
}

function returnToSavedPosition(map) {
  if (previousPosition) {
    map.panTo(previousPosition); // or setCenter
  }
}

... then just call it like:

savePosition(map);

Simple, huh?

like image 67
Chris Broadfoot Avatar answered Mar 06 '23 13:03

Chris Broadfoot


v3 doesn't have a savePosition() function. You need to use getCenter() and getZoom() to retrieve the current position and then restore that position with setCenter() and setZoom().

like image 43
Ryan Olds Avatar answered Mar 06 '23 15:03

Ryan Olds