0

edit: My question is actually a duplicate of this: google maps v3: center of bounds is different from center of the map

I try to extract the center point of the bounds of a google map. Later I want to reposition the map by this center-point. However the map slightly shifts in north-south-direction.

Am I missing something here? In the plunkr you can see how the map shifts after the panTo()-Call, although it is the exact center as retreived from the map.

https://plnkr.co/edit/zGy3nUShEFG7fxvKTqa9?p=preview

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>

var map;
function initMap() {
  var centerData = {lat: -34.397, lng: 150.644};
  map = new google.maps.Map(document.getElementById('map'), {
    center: centerData,
    zoom: 8
  });

  var initialCenterMarker = new google.maps.Marker({
    position: centerData,
    map: map,
    animation:google.maps.Animation.DROP
  });    


  setTimeout(function(){
    var bounds = map.getBounds();
    var myCenter=bounds.getCenter();

    setTimeout(function(){
      map.panTo(myCenter);
      var newCenterMarker = new google.maps.Marker({
        position: myCenter,
        map: map,
        animation:google.maps.Animation.DROP
      });    
    },2000);

  },1000);

}

    </script>
    <script src="https://maps.googleapis.com/maps/api/js?callback=initMap"
        async defer></script>
  </body>
</html>
Community
  • 1
  • 1
Tobias Gassmann
  • 11,399
  • 15
  • 58
  • 92

1 Answers1

2

I think you should use map.getCenter instead of bounds.getCenter()

 map.getCenter();
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107