Retrieves the center position when the map moved.

Description

Dragged Map occur the events "center_change", "dragend" and "bounds_changed". If you want to know just where is the center after that, the easyest way is "mapCanvas.getCenter()" method. Below example code shows the way using "google.maps.event.addListener()".

<script type='text/javascript'>

    var mapCanvas;
    function intialize() {
      //Create a map
      mapCanvas = new google.maps.Map(document.getElementById("map_canvas"));
      mapCanvas.setCenter(new google.maps.LatLng(40.803, -74.097));
      
      mapCanvas.setZoom(9);
      mapCanvas.setMapTypeId(google.maps.MapTypeId.ROADMAP);
      
      var infoWnd = new google.maps.InfoWindow({
        content :  mapCanvas.getCenter().toUrlValue(),
        position : mapCanvas.getCenter(),
        disableAutoPan: true
      });
      infoWnd.open(mapCanvas);
  
      //Retrive the center location
      google.maps.event.addListener(mapCanvas, "center_changed", function() {
        infoWnd.setContent(mapCanvas.getCenter().toUrlValue());
        infoWnd.setPosition(mapCanvas.getCenter());
        infoWnd.open(mapCanvas);
      });
    }
    google.maps.event.addDomListener(window, "load", intialize);
  </script>





Search
Books
Google Maps API Programming Guide

Four Google Maps API Experts wrote this book. Introducing useful examples for developers!
See more details