Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8846025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:52:33+00:00 2026-06-14T11:52:33+00:00

In the Google Maps API V3, I’ve created a map object: map = new

  • 0

In the Google Maps API V3, I’ve created a map object:

map = new google.maps.Map(document.getElementById("map_canvas"),
  myOptions);

I will zoom in and pan on that map and to go back to the original view later, I’d like to save the zoom level and the center of the map. I try the following:

oldCenter = map.getCenter();
oldZoom = map.getZoom();

But the variables stay ‘undefined’. When I do the same thing in the console, I get the correct responses.

What am I doing wrong? Please let me know if more code is needed to find the answer or if it’s an obvious problem.

Thanks!

Full Code:

function initialize() {

  // CUSTOM PLACES
  var latlng = new google.maps.LatLng(51, 10);
  var germany = new google.maps.LatLng(51, 10);
  var myLatlng = new google.maps.LatLng(49,12);

  // DEFINE STYLE
  var styles = [
    {
      "stylers": [
        { "invert_lightness": true }
      ]
    }
  ];

  // MARKER STYLES
  var coin_image  = 'coin.png';
  var merch_image = 'merch.png';

  // DEFINE OPTIONS FOR MAP
  var myOptions = {
    panControl: false,
    zoomControl: true,
    zoomControlOptions: {
      style: google.maps.ZoomControlStyle.SMALL,
      position: google.maps.ControlPosition.LEFT_TOP
    },
    mapTypeControl: false,
    scaleControl: false,
    streetViewControl: false,
    overviewMapControl: false,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };


  // CREATE MAP OBJECT
  map = new google.maps.Map(document.getElementById("map_canvas"),
      myOptions);

  map.setOptions({styles: styles});

  // select zoom, etc by defining 2 points
  var southWest = new google.maps.LatLng(45,-10);
  var northEast = new google.maps.LatLng(55,15);
  var bounds = new google.maps.LatLngBounds(southWest,northEast);
  map.fitBounds(bounds);

  placeMarker(southWest);
  placeMarker(northEast);

  // Place Random Markers
  var lngSpan = northEast.lng() - southWest.lng();
  var latSpan = northEast.lat() - southWest.lat();
  for (var i = 0; i < 50; i++) {
    var location = new google.maps.LatLng(southWest.lat() + latSpan * Math.random(),
        southWest.lng() + lngSpan * Math.random());
    var marker = new google.maps.Marker({
        position: location, 
        map: map,
        icon: merch_image
    });
    var j = i + 1;
    marker.setTitle(j.toString());
  }


  // TRANSACTION MARKERS

  // ONE FULL CYCLE
  // set marker

  var trans_marker = new google.maps.LatLng(52.31799,13.241904);
  var marker = new google.maps.Marker({
      position: trans_marker, 
      map: map,
      animation: google.maps.Animation.DROP,
      title:"Hello World!",
      icon: coin_image
  });

      // HERE'S THE PROBLEM
  // var oldCenter = map.getCenter();
  // var oldZoom = map.getZoom();

  console.log(map);
  oldMap = map;
  console.log(oldMap);
  // console.log(oldZoom);
  // console.log(oldCenter.toString());

  // pan to marker
  setTimeout(function() {map.panTo(trans_marker)}, startDelayed(3000));

  // zoom in on marker
  setTimeout(function() {zoomIn(ENDZOOM)}, startDelayed(1000));

  // show info window
  var contentString =  "<h3>Döner @ Coco Banh</h3>";
  contentString += ("<p>SumUp was used to pay for a Döner at Coco Banh in Berlin, Germany</p>");
  var infowindow = new google.maps.InfoWindow({
    content: contentString
  });

  setTimeout(function() {infowindow.open(map,marker)}, startDelayed(8000));
  setTimeout(function() {infowindow.close()}, startDelayed(5000));  

  // zoom out again
  setTimeout(function() {zoomOut(oldZoom)}, startDelayed(2000));

  // center again
  setTimeout(function() {map.panTo(oldCenter)}, startDelayed(8000));

}
    infowindow = new google.maps.InfoWindow({
   content: contentString
});


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:52:34+00:00Added an answer on June 14, 2026 at 11:52 am

    The problem is that the bounds and center are not set yet. To get them you need to do it in a listener on zoom_changed (for zoom); center_changed (for center). Available Map events are listed in the documentation, under events. You can use the addListenerOnce to only do it once (the first time).

    Something like this will work:

    var oldZoom = null;
    var oldCenter = null;
    google.maps.event.addListenerOnce(map, "zoom_changed", function() { oldZoom = map.getZoom(); });
    google.maps.event.addListenerOnce(map, "center_changed", function() { oldCenter = map.getCenter(); });
    

    proof of concept fiddle

    You won’t be able to use them until after those events have fired.

    code snippet:

    var map;
    var ENDZOOM = 0;
    
    function initialize() {
    
      // CUSTOM PLACES
      var latlng = new google.maps.LatLng(51, 10);
      var germany = new google.maps.LatLng(51, 10);
      var myLatlng = new google.maps.LatLng(49, 12);
    
      // DEFINE STYLE
      var styles = [{
        "stylers": [{
          "invert_lightness": true
        }]
      }];
    
      // MARKER STYLES
      var coin_image = 'http://maps.google.com/mapfiles/ms/micons/blue.png';
      var merch_image = 'http://maps.google.com/mapfiles/ms/micons/yellow.png';
    
      // DEFINE OPTIONS FOR MAP
      var myOptions = {
        panControl: false,
        zoomControl: true,
        zoomControlOptions: {
          style: google.maps.ZoomControlStyle.SMALL,
          position: google.maps.ControlPosition.LEFT_TOP
        },
        mapTypeControl: false,
        scaleControl: false,
        streetViewControl: false,
        overviewMapControl: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
    
    
      // CREATE MAP OBJECT
      map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
    
      map.setOptions({
        styles: styles
      });
    
      // select zoom, etc by defining 2 points
      var southWest = new google.maps.LatLng(45, -10);
      var northEast = new google.maps.LatLng(55, 15);
      var bounds = new google.maps.LatLngBounds(southWest, northEast);
      map.fitBounds(bounds);
    
      placeMarker(southWest);
      placeMarker(northEast);
    
      // Place Random Markers
      var lngSpan = northEast.lng() - southWest.lng();
      var latSpan = northEast.lat() - southWest.lat();
      for (var i = 0; i < 50; i++) {
        var location = new google.maps.LatLng(southWest.lat() + latSpan * Math.random(),
          southWest.lng() + lngSpan * Math.random());
        var marker = new google.maps.Marker({
          position: location,
          map: map,
          icon: merch_image
        });
        var j = i + 1;
        marker.setTitle(j.toString());
      }
    
    
      // TRANSACTION MARKERS
    
      // ONE FULL CYCLE
      // set marker
    
      var trans_marker = new google.maps.LatLng(52.31799, 13.241904);
      var marker = new google.maps.Marker({
        position: trans_marker,
        map: map,
        animation: google.maps.Animation.DROP,
        title: "Hello World!",
        icon: coin_image
      });
    
      var oldZoom = null;
      var oldCenter = null;
      google.maps.event.addListenerOnce(map, "zoom_changed", function() {
        oldZoom = map.getZoom();
        console.log(oldZoom);
        console.log(oldCenter.toString());
      });
      google.maps.event.addListenerOnce(map, "center_changed", function() {
        oldCenter = map.getCenter();
      });
    
    
    
      console.log(map);
      oldMap = map;
      console.log(oldMap);
    
    
      // pan to marker
      setTimeout(function() {
        map.panTo(trans_marker)
      }, startDelayed(3000));
    
      // zoom in on marker
      setTimeout(function() {
        zoomIn(ENDZOOM)
      }, startDelayed(1000));
    
      // show info window
      var contentString = "<h3>Döner @ Coco Banh</h3>";
      contentString += ("<p>SumUp was used to pay for a Döner at Coco Banh in Berlin, Germany</p>");
      var infowindow = new google.maps.InfoWindow({
        content: contentString
      });
    
      setTimeout(function() {
        infowindow.open(map, marker)
      }, startDelayed(8000));
      setTimeout(function() {
        infowindow.close()
      }, startDelayed(5000));
    
      // zoom out again
      setTimeout(function() {
        zoomOut(oldZoom)
      }, startDelayed(2000));
    
      // center again
      setTimeout(function() {
        map.panTo(oldCenter)
      }, startDelayed(8000));
    
    
      infowindow = new google.maps.InfoWindow({
        content: contentString
      });
    }
    google.maps.event.addDomListener(window, "load", initialize);
    
    function placeMarker(latlng) {
      var marker = new google.maps.Marker({
        position: latlng,
        map: map
      })
    }
    
    function startDelayed() {};
    
    function zoomIn(zoom) {};
    
    function zoomOut(zoom) {};
    html,
    body,
    #map_canvas {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js?libraries=geometry&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
    <div id="map_canvas"></div>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With google maps api (utlizing there new Places Library)Im trying to: Display a map
Google Maps (API v3) supports custom controls . E.g. var controlDiv = document.createElement('DIV'); map.controls[google.maps.ControlPosition.TOP_RIGHT].push(controlDiv);
In google maps API v3, I'm creating my own custom map types by calling
Google Maps API v2 vs Google Maps API v3? why Google create a new
In Google Maps API v2, I was using map.clearOverlays() to remove the marker and
I'm using Google maps api v3. In the middle of the map there is
I am using google maps api to place markers on a map. The gps
I'm using the Google Maps API to build a map of store locations near
I use a simple Google Maps API on a simple webpage. The map contains
I'm using Google Maps API V3 and my page loads a map with 5

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.