Click here to see your recently viewed pages and most viewed pages.
Hide
Google Maps JavaScript API

Adding state to controls

View this example full screen.

JavaScript
var map;
var chicago = new google.maps.LatLng(41.85, -87.65);

/**
 * The CenterControl adds a control to the map that recenters the map
 * on Chicago.
 * @constructor
 */

// Define a property to hold the center state
CenterControl.prototype.center_ = null;

// Define setters and getters for this property
CenterControl.prototype.getCenter = function() {
  return this.center_;
}

CenterControl.prototype.setCenter = function(center) {
  this.center_ = center;
}

/** @constructor */
function CenterControl(controlDiv, map, center) {

  // We set up a variable for this since we're adding event listeners later.

  var control = this;

  // Set the center property upon construction
  control.center_ = center;
  controlDiv.style.clear = 'both';

  // Set CSS for the control border
  var goCenterUI = document.createElement('div');
  goCenterUI.style.backgroundColor = '#fff';
  goCenterUI.style.border = '2px solid #fff';
  goCenterUI.style.borderRadius = '3px';
  goCenterUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
  goCenterUI.style.cursor = 'pointer';
  goCenterUI.style.float = 'left';
  goCenterUI.style.marginBottom = '22px';
  goCenterUI.style.textAlign = 'center';
  goCenterUI.title = 'Click to recenter the map';
  controlDiv.appendChild(goCenterUI);

  // Set CSS for the control interior
  var goCenterText = document.createElement('div');
  goCenterUI.style.color = 'rgb(25,25,25)';
  goCenterUI.style.fontFamily = 'Roboto,Arial,sans-serif';
  goCenterUI.style.fontSize = '16px';
  goCenterUI.style.lineHeight = '38px';
  goCenterUI.style.paddingLeft = '5px';
  goCenterUI.style.paddingRight = '5px';
  goCenterUI.innerHTML = 'Center Map';
  goCenterUI.appendChild(goCenterText);

  // Set CSS for the setCenter control border
  var setCenterUI = document.createElement('div');
  setCenterUI.style.backgroundColor = '#fff';
  setCenterUI.style.border = '2px solid #fff';
  setCenterUI.style.borderRadius = '3px';
  setCenterUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
  setCenterUI.style.cursor = 'pointer';
  setCenterUI.style.float = 'left';
  setCenterUI.style.marginBottom = '22px';
  setCenterUI.style.marginLeft = '12px';
  setCenterUI.style.textAlign = 'center';
  setCenterUI.title = 'Click to change the center of the map';
  controlDiv.appendChild(setCenterUI);

  // Set CSS for the control interior
  var setCenterText = document.createElement('div');
  setCenterText.style.color = 'rgb(25,25,25)';
  setCenterText.style.fontFamily = 'Roboto,Arial,sans-serif';
  setCenterText.style.fontSize = '16px';
  setCenterText.style.lineHeight = '38px';
  setCenterText.style.paddingLeft = '5px';
  setCenterText.style.paddingRight = '5px';
  setCenterText.innerHTML = 'Set Center';
  setCenterUI.appendChild(setCenterText);

  // Setup the click event listener for 'Center':
  // simply set the map to the control's current center property.
  google.maps.event.addDomListener(goCenterUI, 'click', function() {
    var currentCenter = control.getCenter();
    map.setCenter(currentCenter);
  });

  // Setup the click event listener for 'Set':
  // Set the control's center to the current Map center.
  google.maps.event.addDomListener(setCenterUI, 'click', function() {
    var newCenter = map.getCenter();
    control.setCenter(newCenter);
  });
}

function initialize() {
  var mapDiv = document.getElementById('map-canvas');
  var mapOptions = {
    zoom: 12,
    center: chicago
  }
  map = new google.maps.Map(mapDiv, mapOptions);

  // Create the DIV to hold the control and
  // call the CenterControl() constructor passing
  // in this DIV.
  var centerControlDiv = document.createElement('div');
  var centerControl = new CenterControl(centerControlDiv, map, chicago);

  centerControlDiv.index = 1;
  map.controls[google.maps.ControlPosition.BOTTOM_LEFT].push(centerControlDiv);
}

google.maps.event.addDomListener(window, 'load', initialize);
JavaScript + HTML
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Adding state to controls</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
    <script>

var map;
var chicago = new google.maps.LatLng(41.85, -87.65);

/**
 * The CenterControl adds a control to the map that recenters the map
 * on Chicago.
 * @constructor
 */

// Define a property to hold the center state
CenterControl.prototype.center_ = null;

// Define setters and getters for this property
CenterControl.prototype.getCenter = function() {
  return this.center_;
}

CenterControl.prototype.setCenter = function(center) {
  this.center_ = center;
}

/** @constructor */
function CenterControl(controlDiv, map, center) {

  // We set up a variable for this since we're adding event listeners later.

  var control = this;

  // Set the center property upon construction
  control.center_ = center;
  controlDiv.style.clear = 'both';

  // Set CSS for the control border
  var goCenterUI = document.createElement('div');
  goCenterUI.style.backgroundColor = '#fff';
  goCenterUI.style.border = '2px solid #fff';
  goCenterUI.style.borderRadius = '3px';
  goCenterUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
  goCenterUI.style.cursor = 'pointer';
  goCenterUI.style.float = 'left';
  goCenterUI.style.marginBottom = '22px';
  goCenterUI.style.textAlign = 'center';
  goCenterUI.title = 'Click to recenter the map';
  controlDiv.appendChild(goCenterUI);

  // Set CSS for the control interior
  var goCenterText = document.createElement('div');
  goCenterUI.style.color = 'rgb(25,25,25)';
  goCenterUI.style.fontFamily = 'Roboto,Arial,sans-serif';
  goCenterUI.style.fontSize = '16px';
  goCenterUI.style.lineHeight = '38px';
  goCenterUI.style.paddingLeft = '5px';
  goCenterUI.style.paddingRight = '5px';
  goCenterUI.innerHTML = 'Center Map';
  goCenterUI.appendChild(goCenterText);

  // Set CSS for the setCenter control border
  var setCenterUI = document.createElement('div');
  setCenterUI.style.backgroundColor = '#fff';
  setCenterUI.style.border = '2px solid #fff';
  setCenterUI.style.borderRadius = '3px';
  setCenterUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
  setCenterUI.style.cursor = 'pointer';
  setCenterUI.style.float = 'left';
  setCenterUI.style.marginBottom = '22px';
  setCenterUI.style.marginLeft = '12px';
  setCenterUI.style.textAlign = 'center';
  setCenterUI.title = 'Click to change the center of the map';
  controlDiv.appendChild(setCenterUI);

  // Set CSS for the control interior
  var setCenterText = document.createElement('div');
  setCenterText.style.color = 'rgb(25,25,25)';
  setCenterText.style.fontFamily = 'Roboto,Arial,sans-serif';
  setCenterText.style.fontSize = '16px';
  setCenterText.style.lineHeight = '38px';
  setCenterText.style.paddingLeft = '5px';
  setCenterText.style.paddingRight = '5px';
  setCenterText.innerHTML = 'Set Center';
  setCenterUI.appendChild(setCenterText);

  // Setup the click event listener for 'Center':
  // simply set the map to the control's current center property.
  google.maps.event.addDomListener(goCenterUI, 'click', function() {
    var currentCenter = control.getCenter();
    map.setCenter(currentCenter);
  });

  // Setup the click event listener for 'Set':
  // Set the control's center to the current Map center.
  google.maps.event.addDomListener(setCenterUI, 'click', function() {
    var newCenter = map.getCenter();
    control.setCenter(newCenter);
  });
}

function initialize() {
  var mapDiv = document.getElementById('map-canvas');
  var mapOptions = {
    zoom: 12,
    center: chicago
  }
  map = new google.maps.Map(mapDiv, mapOptions);

  // Create the DIV to hold the control and
  // call the CenterControl() constructor passing
  // in this DIV.
  var centerControlDiv = document.createElement('div');
  var centerControl = new CenterControl(centerControlDiv, map, chicago);

  centerControlDiv.index = 1;
  map.controls[google.maps.ControlPosition.BOTTOM_LEFT].push(centerControlDiv);
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>