Google Maps JavaScript API v3

Drawing Library

Overview

The DrawingManager class provides a graphical interface for users to draw polygons, rectangles, polylines, circles, and markers on the map.

Using the Library

The Drawing Tools are a self-contained library, separate from the main Maps API JavaScript code. To use the functionality contained within this library, you must first load it using the libraries parameter in the Maps API bootstrap URL:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=drawing&sensor=true_or_false"></script>

Once you've added the libraries parameter, you can create a DrawingManager object as follows:

var drawingManager = new google.maps.drawing.DrawingManager();
drawingManager.setMap(map);

DrawingManager Options

The DrawingManager constructor takes a set of options that define the set of controls to display, the position of the control, and the initial drawing state.

  • The drawingMode property of the DrawingManager defines the initial drawing state of the DrawingManager. It accepts a google.maps.drawing.OverlayType constant. Default is null, in which case the cursor is in a non-drawing mode when the DrawingManager is initialized.
  • The drawingControl property of the DrawingManager defines the visibility of the drawing tools selection interface on the map. It accepts a boolean value.
  • You can also define the position of the control, and the types of overlays that should be represented in the control, using the drawingControlOptions property of the DrawingManager.
    • position defines the position of the drawing control on the map, and accepts a google.maps.ControlPosition constant.
    • drawingModes is an array of google.maps.drawing.OverlayType constants, and defines the overlay types to include in the drawing control shape picker. The hand icon will always be present, allowing the user to interact with the map without drawing. The order of the tools in the control will match the order in which they are declared in the array.
  • Each type of overlay can be assigned a set of default properties, that define the appearance of the overlay when first created. These are defined in that overlay's {overlay}Options property (where {overlay} represents the overlay type). For example, a circle's fill properties, stroke properties, zIndex, and clickability can be defined with the circleOptions property. If any size, location, or map values are passed, they are ignored. For full details of which properties can be set, refer to the API Reference documentation.

Note: To make a shape user-editable after it has been created, set its editable property to true.

var drawingManager = new google.maps.drawing.DrawingManager({
  drawingMode: google.maps.drawing.OverlayType.MARKER,
  drawingControl: true,
  drawingControlOptions: {
    position: google.maps.ControlPosition.TOP_CENTER,
    drawingModes: [
      google.maps.drawing.OverlayType.MARKER,
      google.maps.drawing.OverlayType.CIRCLE,
      google.maps.drawing.OverlayType.POLYGON,
      google.maps.drawing.OverlayType.POLYLINE,
      google.maps.drawing.OverlayType.RECTANGLE
    ]
  },
  markerOptions: {
    icon: 'http://www.example.com/icon.png'
  },
  circleOptions: {
    fillColor: '#ffff00',
    fillOpacity: 1,
    strokeWeight: 5,
    clickable: false,
    zIndex: 1,
    editable: true
  }
});
drawingManager.setMap(map);

View example (drawing-tools.html)

Updating the Drawing Tools Control

Once the DrawingManager object has been created, you can update it by calling setOptions() and passing new values.

drawingManager.setOptions({
  drawingControlOptions: {
    position: google.maps.ControlPosition.BOTTOM_LEFT,
    drawingModes: [google.maps.drawing.OverlayType.MARKER]
  }
});

To hide or show the drawing tools control:

// To hide:
drawingManager.setOptions({
  drawingControl: false
});

// To show:
drawingManager.setOptions({
  drawingControl: true
});

To remove the drawing tools control from the map object:

drawingManager.setMap(null);

Hiding the drawing control causes the drawing tools control to not display, but all of the functionality of the DrawingManager class is still available. In this way, you can implement your own control, if desired. Removing the DrawingManager from the map object disables all drawing functionality; it must be reattached to the map with drawingManager.setMap(map), or a new DrawingManager object constructed, if drawing features are to be restored.

Drawing Events

When a shape overlay is created, two events are fired:

  • An {overlay}complete event (where {overlay} represents the overlay type, such as circlecomplete, polygoncomplete, etc). A reference to the overlay is passed as an argument.
  • An overlaycomplete event. An object literal, containing the OverlayType and a reference to the overlay, is passed as an argument.
google.maps.event.addListener(drawingManager, 'circlecomplete', function(circle) {
  var radius = circle.getRadius();
});

google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) {
  if (event.type == google.maps.drawing.OverlayType.CIRCLE) {
    var radius = event.overlay.getRadius();
  }
});

Note that google.maps.Map events, such as click and mousemove are disabled while drawing on the map.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.