Hide
Google Maps JavaScript API v3

Draggable Polygons

View this example full screen.

JavaScript
// This example creates draggable triangles on the map.
// Note also that the red triangle is geodesic, so its shape changes
// as you drag it north or south.

function initialize() {

  var mapOptions = {
    zoom: 1,
    center: new google.maps.LatLng(24.886, -70.268),
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

  var blueCoords = [
    new google.maps.LatLng(25.774, -60.190),
    new google.maps.LatLng(18.466, -46.118),
    new google.maps.LatLng(32.321, -44.757)
  ];

  var redCoords = [
    new google.maps.LatLng(25.774, -80.190),
    new google.maps.LatLng(18.466, -66.118),
    new google.maps.LatLng(32.321, -64.757)
  ];

  // Construct a draggable red triangle with geodesic set to true.
  new google.maps.Polygon({
    map: map,
    paths: redCoords,
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35,
    draggable: true,
    geodesic: true
  });

  // Construct a draggable blue triangle with geodesic set to false.
  new google.maps.Polygon({
    map: map,
    paths: blueCoords,
    strokeColor: '#0000FF',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#0000FF',
    fillOpacity: 0.35,
    draggable: true,
    geodesic: false
  });
}

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>Draggable Polygons</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>
// This example creates draggable triangles on the map.
// Note also that the red triangle is geodesic, so its shape changes
// as you drag it north or south.

function initialize() {

  var mapOptions = {
    zoom: 1,
    center: new google.maps.LatLng(24.886, -70.268),
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

  var blueCoords = [
    new google.maps.LatLng(25.774, -60.190),
    new google.maps.LatLng(18.466, -46.118),
    new google.maps.LatLng(32.321, -44.757)
  ];

// [START region_red_triangle]
  var redCoords = [
    new google.maps.LatLng(25.774, -80.190),
    new google.maps.LatLng(18.466, -66.118),
    new google.maps.LatLng(32.321, -64.757)
  ];

  // Construct a draggable red triangle with geodesic set to true.
  new google.maps.Polygon({
    map: map,
    paths: redCoords,
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35,
    draggable: true,
    geodesic: true
  });
  // [END region_red_triangle]

  // Construct a draggable blue triangle with geodesic set to false.
  new google.maps.Polygon({
    map: map,
    paths: blueCoords,
    strokeColor: '#0000FF',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#0000FF',
    fillOpacity: 0.35,
    draggable: true,
    geodesic: false
  });
}

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

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