Rectangles

Learn how to use rectangle overlays on a map.

The Google Maps JavaScript API also includes a specific class for Rectangle objects that simplifies the construction of rectangles on a map.

Add a rectangle to a map

The Rectangle object is somewhat similar to the Polygon object in that we can define custom styles for both objects. However, unlike polygons, we do not define paths for a Rectangle. Instead, we pass the bounds property, which defines the bounds of the rectangle in each cardinal direction.

The Rectangle constructor takes in the RectangleOptions parameter. That parameter defines the rectangle’s center LatLng coordinates, size, and style.

RectangleOptions parameters

All RectangleOptions parameters are optional since there are no required parameters. Here’s a list of all the optional RectangleOptions parameters that we can pass to the Rectangle constructor:

Parameters

Type

Description

bounds

LatLngBounds | LatLngBoundsLiteral>

This parameter specifies the rectangle's North, South, East, and West bounds.

strokeColor

string

This parameter specifies a hexadecimal code for the color of the line stroke, like #FFFFFF.

strokeOpacity

number

This parameter specifies a numerical value between 0.0 and 1.0 that determines the opacity of the stroke line's color. By default, the strokeOpacity value is set to 1.0.

strokePosition

StrokePosition

This parameter specifies the direction of the stroke. The StrokePosition datatype has three possible constant values—CENTER, INSIDE, and OUTSIDE. By default, the strokePosition value is set to CENTER.


The CENTER value centers the stroke on the rectangle's boundary, rendering one half of the stroke on the inside and one half on the outside of the rectangle. The INSIDE value renders the stroke inside the rectangle, and the OUTSIDE value renders it outside the rectangle.

strokeWeight

number

This parameter specifies the width of the line in pixels.

clickable

boolean

This parameter specifies whether the Rectangle can handle mouse events. By default, the clickable value is set to true.

draggable

boolean

This parameter allows the user to drag the rectangle across the map when it's set to true. By default, the draggable value is set to false.

editable

boolean

When set to true, this parameter allows the user to edit the rectangle by dragging the control points of its vertices and edges. By default, the editable value is set to false.

fillColor

string

This parameter specifies a hexadecimal code for the fill color of the rectangle, like #FFFFFF.

fillOpacity

number

This parameter specifies a numerical value between 0.0 and 1.0 that determines the opacity of the rectangle's fill color. By default, the fillOpacity value is set to 0.35.

map

Map

This parameter specifies the map that will have the rectangle rendered on it.

visible

boolean

This parameter specifies whether the rectangle is displayed on the map or not.

zindex

number

This parameter represents the overlay order in which different drawing objects are placed in front of each other. A smaller-value zIndex object comes in front of a larger-value zIndex object.

We can construct a Rectangle object without any RectangleOptions parameters, since they all are optional. However, if we want to display a Rectangle object, the map and bounds parameters are required, and the visible parameter value must be true.

We can either provide the map and bounds, and all other parameters during construction or after construction using the setValues() method. The setMap() method can also be used to update the map parameter after construction.

Here’s an example of how a Rectangle object is constructed:

new google.maps.Rectangle({
	bounds: {
		north: 41.001, // Latitude coordinate of Colorado's North
		south: 36.971, // Latitude coordinate of Colorado's South
		east: -102.047, // Longitude coordinate of Colorado's East
		west: -109.078, // Longitude coordinate of Colorado's West
	},
	strokeColor: "#FF0000",
	strokeOpacity: 0,
	strokePosition: google.maps.StrokePosition.CENTER,
	strokeWeight: 2,
	clickable: true,
	draggable: false,
	editable: false,
	fillColor: "#FF0000",
	fillOpacity: 0,
	map: map,
	visible: true,
	zIndex: 1,
});

Code example

In the following example, we completely enclose the State of Colorado in the US in a red rectangle on a map:

Console
Add a rectangle to a map

Here’s a brief explanation of the JavaScript file in the code widget above:

  • line 8: We construct a single Rectangle object over the State of Colorado.
  • Lines 9–14: Of all the RectangleOptions parameters we pass, the only one of note is the bounds parameter. In this parameter, we pass separate latitude and longitude in all the cardinal directions to designate the boundaries of the Rectangle to precisely that of Colorado.

Remove a rectangle

We can remove a rectangle from the map by calling the setMap() method on the Rectangle object and passing null as its argument.

newRectangle.setMap(null);

Code example

In the example below, we create a Rectangle object, newRectangle, with a UI control that allows the user to remove the rectangle from the map:

Console
Remove a rectangle

Here’s a brief explanation of the HTML file in the above code widget:

  • Lines 8–11: We create a separate div element just above our map’s div element. We create two input buttons in this separate div element: add-rectangle on line 9 for adding a rectangle and remove-rectangle on line 10 for removing the rectangle.

Here’s a brief explanation of the JavaScript file in the above code widget:

  • Lines 1–2: We define two global variables: map and newRectangle.
  • Line 11: We initialize our Rectangle object, newRectangle. We set the boundaries of Colorado as the bounds of the rectangle such that it completely covers the city.
  • Lines 31–35: We define a function addRectangle() to add the rectangle. When this function is called, the setMap() method on line 32 renders newRectangle over the map.
  • line 37–41: We define a function removeRectangle() to remove the rectangle, and using the setMap() method on line 38, we set the map parameter to null, which removes the rectangle from the map.
  • Line 27: We define an event listener that listens for a click event for the add-rectangle button element and calls the addRectangle() method.
  • Line 28: We define an event listener that listens for a click event for the remove-rectangle button element and calls the removeRectangle() method.