Are there any contextual menus available for Google Maps v3?
Asked
Active
Viewed 1.3k times
5
-
Besides the good examples below, I am still looking for a library I could use, giving me encapsulation and convenient features. Please let me know if you find something like this. – Horst Walter Aug 18 '11 at 20:10
4 Answers
8
You can add context menu as it is decribed here How to add context menu to google maps, using api V3:

RameshVel
- 64,778
- 30
- 169
- 213

user364313
- 96
- 1
- 3
-
The direct link is http://googleapitips.blogspot.com/2010/06/how-to-add-context-menu-to-google-maps.html – Oleksandr Yanovets Jul 14 '10 at 08:08
1
Old question but it came up in my googling so I thought I would post the simplest answer. It's a context menu without the use of more 3rd party js libraries. There's also a latlon object in the event that you can get the lat/lon for where the person clicked, to add a maker or whatever.
var contextMenu = google.maps.event.addListener(
map,
"rightclick",
function (event) {
// use JS Dom methods to create the menu
// use event.pixel.x and event.pixel.y
// to position menu at mouse position
$('.contextmenu').remove(); //remove previous context menus
contextmenuDir = document.createElement("div");
contextmenuDir.className = 'contextmenu';
//now add our options.
contextmenuDir.innerHTML = '<a id="menu1"><div class="context">menu item 1<\/div><\/a>'
+ '<a id="menu2"><div class="context">menu item 2<\/div><\/a>';
$(map.getDiv()).append(contextmenuDir);
contextmenuDir.style.visibility = "visible";
// might need to offset if you have moved the map div like i did (then - the pixel size off)
$('.contextmenu').css('left', event.pixel.x );
$('.contextmenu').css('top', event.pixel.y);
console.log(event); //log some details about the object we get
});

Mark
- 99
- 7
-
> "without the use of more 3rd party js libraries" Looks like you are using Jquery in that code. – Gerry Mar 18 '16 at 15:04
-
ah true, let me re-phase there. It works without extra 3rd party mapping libraries. – Mark Mar 21 '16 at 01:08
-
Note: Official google maps api recommends using the 'contextmenu' event rather than the 'rightclick'. I'm still looking for a simple example using the contextmenu event... – Ringo Aug 25 '21 at 17:27