Google Maps API Tutorial

© 2006, 2007, 2008, 2009 Mike Williams

 
Translate this page:

 

GEvents

Here's a list of the accessible GEvent types.
 
EventObjectParameters
"addmaptype" GMap2, GMap map type
"addoverlay" GMap2, GMap overlay
"clearoverlays"GMap2, GMap -
"click" GMap2, GMap overly, latlng, overlaylatlng
"dblclick"GMap2, GMap the first parameter is always null
The second parameter is the GLatLng
"drag"GMap2, GMap -
"dragend"GMap2, GMap -
"dragstart"GMap2, GMap -
"infowindowbeforeclose"GMap2, GMap -
"infowindowclose"GMap2, GMap -
"infowindowopen"GMap2, GMap -
"load"GMap2, GMap -
"maptypechanged"GMap2, GMap -
"move"GMap2, GMap -
"moveend"GMap2, GMap -
"movestart"GMap2, GMap -
"mousemove"GMap2, GMap GLatLng
"mouseout"GMap2, GMap GLatLng
"mouseover"GMap2, GMap GLatLng
"removemaptype" GMap2, GMap map type
"removeoverlay" GMap2, GMap overlay
"resize"GMap2, GMap -
"singlerightclick"GMap2, GMap 1. GLatLng() identifying the pixel position of the mouse within the map div.
2. URL of tile (if over the base map)
3. Map container if over the base map. Reference to the Gmarker if over a marker.
"zoomend"GMap2, GMap old zoom level, new zoom level
"zooming"GMap2, GMap -
"zoomstart"GMap2, GMap direction (+1 = zoom in, -1 = zoom out)
latlng pivot
bool recentering
EventObjectParameters
"zoom"GMap old zoom level, new zoom level (v1 syntax)
EventObjectParameters
"newcopyright" GMapType copyright
EventObjectParameters
"newcopyright" GTileLayer copyright
EventObjectParameters
"newcopyright" GCopyrightCollection copyright
EventObjectParameters
"click" GMarker latlng of marker
"changed" GMarker marker, previous position
"dblclick" GMarker latlng of marker
"drag"GMarker latlng
"dragend"GMarker latlng
"dragstart"GMarker latlng
"infowindowbeforeclose"GMarker marker
"infowindowclose"GMarker marker
"infowindowopen"GMarker marker
"infowindowprepareopen"GMarker marker
"mousedown" GMarker latlng of marker
"mouseover" GMarker latlng of marker
"mouseout" GMarker latlng of marker
"mouseup" GMarker latlng of marker
"remove" GMarker -
"visibilitychanged" GMarker true if the marker is now visible, else false
EventObjectParameters
"changed" GMarkerManager GBounds indicating active region in units of 1024x1024 pixels, number of active markers
EventObjectParameters
"cancelline"GPolyline -
"click"GPolyline GLatLng of clicked location
"endline"GPolyline -
"lineupdated"GPolyline -
"mouseout"GPolyline -
"mouseover"GPolyline -
"remove"GPolyline -
"visibilitychanged"GPolyline true if the poly is now visible, else false
EventObjectParameters
"cancelline"GPolygon -
"click"GPolygon GLatLng of clicked location
"endline"GPolygon -
"lineupdated"GPolygon -
"mouseout"GPolygon -
"mouseover"GPolygon -
"remove"GPolygon -
"visibilitychanged"GPolygon true if the poly is now visible, else false
EventObjectParameters
"moveend"GKeyboardHandler -
"movestart"GKeyboardHandler -
EventObjectParameters
"animate" [GInfoWindow] proportion of the animation that has been performed
"closeclick" [GInfoWindow] -
"maximizeclick" [GInfoWindow] -
"maximizeend" [GInfoWindow] -
"restoreclick" [GInfoWindow] -
"restoreend" [GInfoWindow] -
EventObjectParameters
"click" GDraggableObject browser-specific event object
"dblclick" GDraggableObject browser-specific event object
"drag" GDraggableObject browser-specific event object
"dragend" GDraggableObject browser-specific event object
"dragstart" GDraggableObject browser-specific event object
"mouseup" GDraggableObject browser-specific event object
"mousedown" GDraggableObject browser-specific event object
"move" GDraggableObject -
EventObjectParameters
"visibilitychanged"GScreenOverlay true if the overlay is now visible, else false
EventObjectParameters
"visibilitychanged"GGroundOverlay true if the overlay is now visible, else false
EventObjectParameters
"load"GGeoXml -
EventObjectParameters
"changed"GTrafficOverlay true if the viewport contains traffic data
EventObjectParameters
"error"GStreetViewPanorama error code
"initialized"GStreetViewPanorama location object
"pitchchanged"GStreetViewPanorama pitch
"yawchanged"GStreetViewPanorama yaw
"zoomchanged"GStreetViewPanorama zoom
EventObjectParameters
"changed"GStreetViewOverlay true if the viewport contains StreetView data

Notes

  1. Watch out for those parameters. The same event can have different parameters with different objects.
  2. The GInfoWindow class isn't directly accessible, but you can use GEvent.addListener(map.getInfoWindow(),"closeclick" ...).
  3. The "closeclick" event is only triggered when the user actually clicks on the info window close icon, not for anything else that closes the info window.
  4. Watch out for the difference between the "zoom" event (GMap() only) which returns zoom levels in APIv1 format and the "zoomend" event which returns zoom levels in APIv2 format.
  5. To differentiate the situation where the user moves the map rather than the map being moved by code, you can use drag* and dblclick events. The move* events are triggered by anything that moves the map, which can include calls made by your own Javascript code and the opening of an info window.

Custom Events

It's also possible to use the GEvent system to pass messages between different parts of your own code with custom events. Simply use GEvent.addListener(marker, "myevent") to listen for the event and GEvent.trigger(marker, "myevent") to trigger it. You can pass up to two parameters.

You can pass these custom events on any object, these can be API objects, HTML elements, data structures, or any type of Object().

   GEvent.addListener(map, "myevent", ...
   GEvent.addListener(document.getElementById("message"), "thisevent", ...
   var fred = new Object();
   GEvent.addListener(fred, "thatevent", ...
   

GEvent.addDomListener

It's also possible to use GEvent.addDomListener() to listen for Dom events on HTML Elements.

Writing GEvent.addDomListener(document.getElementById("button"), "mouseover", ... has a similar effect to writing <input id="button" onmouseover="...">, but there are one or two advantages to using the GEvent system:

  1. You may find it easier to add or remove GEvent listeners
  2. You can use GEvent.trigger on these events
You can use this system to handle any of the standard HTML Dom events: "blur", "click", "dblclick", "focus", "keydown", "keypress", "keyup", "load", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup".

When using this system, the full event details are passed in the first parameter. So for example you could write

GEvent.addDomListener(document.getElementById("map"), "click", function(e) {...
and then examine the details of the event "e" and behave differently if, say, the ALT or CTRL keys were pressed by testing if (e.altKey) or if (e.ctrlKey).

Note that if you write GEvent.addDomListener(map, "click" then you don't get the Dom event because a GMap2() object isn't a HTML element. You get the conventional "map click" event instead, with the parameters "overlay" and "latlng".

Back to Mike's Homepage
Up to the start of this tutorial