Introduction to Markers
Learn how to add and customize markers that identify a location on a map.
We'll cover the following
Markers make it possible to identify locations on a map. They are an excellent way to highlight important places by setting custom icons. This lesson focuses on the basics of how to add or remove markers and make them draggable.
Add a simple marker
To add a marker on a map, we must create a map first. We use the google.maps.Marker
constructor to add a marker. It takes an object that specifies the initial marker properties as input.
The Marker
constructor takes in the MarkerOptions
parameter. This parameter defines the LatLng
coordinates for its position and alters the marker’s visual features with a style set.
The MarkerOptions
parameters
All MarkerOptions
parameters are optional, so there are no required parameters. Here’s a list of all optional MarkerOptions
parameters that we can pass to the Marker
constructor:
Parameters | Type | Description |
|
| This parameter specifies the initial location of the marker using the latitude and longitude coordinates. |
|
| This parameter specifies the map that will have markers rendered on it. |
|
| This parameter is used when an info window with the marker as an anchor opens. It specifies the offset between the tip of the info window and the marker's position. |
|
| This parameter specifies the type of animation that plays when a marker is added to the map. The By default, the Animation value is The The |
|
| This parameter specifies whether to disable the cross that appears beneath the marker when its dragged. By default, the editable value is |
|
| This parameter specifies the type of cursor that appears when hovering over the marker. |
|
| This parameter specifies whether the |
|
| This parameter allows the user to drag the markers across the map when it is set to |
|
| This parameter specifies an icon for the map foreground that replaces the default Google Maps marker icon. When a string is passed, that string is used as a URL for the icon. |
|
| This parameter specifies the label to display with the marker. This label is either a letter or number that will appear inside a marker. |
|
| This parameter specifies a numerical value between |
|
| This parameter is used for optimization when too many markers are on the map. It improves map performance by rendering markers as a single static element. |
|
| This parameter specifies the clickable region of the marker. The |
|
| This parameter specifies the marker's title, which is rendered as rollover text. |
|
| This parameter specifies whether the markers are displayed on the map or not. |
|
|
|
We can construct a Marker
object without any parameters since all parameters are optional. However, to display a Marker
, we must provide the map
and the position
parameters and set the visible
parameter value as true
.
All other parameters, including map
and position
, can be provided during construction or after construction using the setValues()
method. We can also use setMap()
to update the map parameter.
Here’s an example of how to construct a Marker
:
new google.maps.Marker({position: { lat: 29.9792, lng: 31.1342 }, // Coordinates of the Great Pyramid of Gizamap: map,anchorPoint: google.maps.Point(50, 50), // An info window with anchor set on current markeranimation: google.maps.Animation.DROP,crossOnDrag: true,cursor: "crosshair",clickable: true,draggable: true,//icon: "https://developers.google.com/maps/documentation/javascript/examples/full/images/",label: "1",opacity: 1,optimized: false,shape: { coords: [25, 25, 25], type: "circle" },title: "Great Pyramid of Giza",visible: true,zIndex: 1,});
Code example
The example below adds a simple marker to identify Mount Everest on a map:
We place the marker on the map using the marker’s map
property. Here’s a brief explanation of the JavaScript file in the code widget above:
- Line 4: We declare the
everest
variable to store the coordinates of Mount Everest. - Lines 13–17: We define a
Marker
object and set theeverest
coordinates as itsposition
. - Line 16: We set the
draggable
property totrue
, which allows users to drag a marker to a different location on the map. - Line 24: We attach a marker to the map using the
setMap()
method. - Comment out lines 13-17 and uncomment lines 19-24 to facilitate the testing of the
setMap()
method.
Remove a marker
To remove an existing marker, set the marker object as null
, effectively deleting it from the map. This is done by calling the setMap()
method and passing null
as an argument, like this:
marker.setMap(null);
Code example
In the example below, a centered marker is added at the given coordinates on the map when the user clicks on the “Add Marker” button. The center marker is removed when the user click the “Remove Marker” button:
Here’s a breakdown of the HTML file in the code widget above:
- Lines 8–11: We create a separate
div
element just above the map’sdiv
element. - Line 9: We create an input button whose
id
isadd-marker
. This button adds the marker. - Line 10: We create another input button, whose
id
isremove-marker
. It removes the marker.
Here’s a breakdown of the JavaScript file in the code widget above:
-
Line 22: We add an event listener to identify the
click
event that adds a marker when the “Add Marker” button is clicked on the map. This calls theaddMarker()
method. -
Line 25: We add an event listener to identify the
click
event to remove the marker when we click on the “Remove Marker” button. This calls theremoveMarker()
method. -
Lines 29–33: The addMarker() method is defined.
-
Line 30: The
setMap()
method renders themarker
object over the map. -
Lines 36–40: The
removeMarker()
method is defined, which removes themarker
object by setting itsmap
parameter tonull
.