Simple Events
Learn how to handle simple events on a map using event listeners.
We'll cover the following...
Javascript is an event-driven programming language we use within a browser. This means that Javascript responds to interactions by generating events and expects a program to listen to these UI events.
There are two types of UI events, which are as follows:
- State change events reflect changes in certain Maps JavaScript API objects. When an object’s property changes, an event is fired that states that the property has changed. For example, a
zoom_changedevent will be fired when the map’s zoom level changes. - User events are propagated from the DOM to the Maps JavaScript API. An example is a mouse
clickevent.
UI Events
Objects in the Maps JavaScript API respond to user events such as mouse events or keyboard events. Some of the events that correspond to the map object include the following:
- The
bounds_changedevent triggers whenever the map view bounds are changed. - The
center_changedevent triggers whenever the center of the map changes. - The
clickevent triggers whenever a user clicks on any clickable object on the map or on the map itself. - The
contextmenuevent triggers whenever the user tries to open the DOM context menu event in the map container. - The
dblclickevent triggers whenever the user double-clicks on the map. - The
dragevent triggers whenever the user holds down and drags the map. - The
dragendevent triggers whenever the user stops dragging the map. - The
dragstartevent triggers whenever the user starts dragging the map. - The
heading_changedevent triggers whenever the heading attribute of the map changes. - The
idleevent triggers whenever the map is left idle after zooming or panning. - The
maptypeid_changedevent triggers whenever the user changes the map type. - The
mousemoveevent triggers whenever the user moves the pointer across the map. - The
mouseoutevent triggers whenever the user moves the pointer off of the map container. - The
mouseoverevent triggers whenever the user moves the pointer over the map container from outside. - The
projection_changedevent triggers whenever the projection changes on the map. - The
rightclickevent triggers whenever the user right-clicks on the map. - The
tilesloadedevent triggers whenever the map tiles have finished loading. - The
tilt_changedevent triggers whenever the tilt of the map changes. - The
zoom_changedevent triggers whenever the zoom changes on the map.
Event handling
The addListener() event handler is used to register for event notifications. This method listens for an event and a function that’s called when the event occurs as parameters. Here’s how to add a click listener to a Map object called map:
map.addListener("click", () => {
// Perform action on click event trigger
});
Code example
In the example below, event listeners are attached to listen for whenever the marker is clicked or the map is panned across.
Here’s a brief explanation of the JavaScript file in the above code widget:
- Line 8: We initialize a
Markerobject calledmarkerso that we can test our click events on it. - Line 15: We add an event listener to the
mapobject to listen for thecenter_changedevent, which occurs whenever the map center is changed by dragging or panning the map to another map center. - Lines 16–18: The event listener for
center_changedtriggers a timeout function. This timeout function pans the map back to the marker location using thepanTo()method three seconds after thecenter_changedevent occurs. - Lines 23–26: We add a listener for any
clickevents on the marker. The listener handles theclickevent by first changing the mapzoomfrom4to8and then changing the map center to that of the marker if the map is not already centered on it.