Google Maps API Tutorial© 2007, 2008, 2009 Mike Williams
|Translate this page:|
Context MenuYou may have noticed that maps.google.com now has a context menu that can be invoked with a right click.
It is possible to create context menus for your own API maps by using the "singlerightclick" event.
Here's an Example
There's nothing in the API to help you construct the context menu itself. You have to write your own code to do that.
What I basically do in that example, is to create a hidden div that contains the menus and has onclicks that each call a global function. When the user right-clicks the map, the "singlerightclick" event is triggered, and it returns a GPoint() indicating the pixel position of the click relative to the map container.
When the code detects such an event, I:
Whenever there's a click on the map or one of its clickable overlays, I close the context menu.
If one of the functions needs to know where on the map the right-click occurred, I read the pixel location from the global variable and convert it to a GLatLng with map.fromContainerPixelToLatLng().
The strange looking divs inside the links are there to make the whole width of the menu clickable.
You can obviously get your context menus to contain whatever options you want. You could even generate the .innerHTML of the context div dynamically when the right click occurs, with different options depending on the context.
Bits I've not figured out yet