1

I've been using the Google Earth plugin to build specific functionality and one of the things I'd like to do is create my own context menu over GE.

Has anyone done this? Any help is greatly appreciated.

JGood
  • 522
  • 6
  • 23

2 Answers2

1

Currently, you need to use IFRAME SHIMS. Hopefully that will change one day.

Check out this page for an example http://earth-api-samples.googlecode.com/svn/trunk/demos/customcontrols/index.html

and check out this other question on SO for some more info How can I place a html div over the Google Earth plugin? Involves wmode, I imagine

If you are interested, you can see my webpage which uses iframe shims over google earth here http://www.3dwhistler.com/

Community
  • 1
  • 1
lifeIsGood
  • 1,229
  • 1
  • 15
  • 22
1

You could achieve this by listening for mouseup or click event and then using a shim overlay technique to display a custom context menu.

The code for the event listener would be something like the following.

// Listen for all mouseup events in the plugin.
// Where 'ge' is an instance of the GEPlugin
google.earth.addEventListener(ge.getWindow(), 'mouseup', eventHandler);

The event handler would be something like the following.

// Handles mouseup events (e is a KmlMouseEvent)
var eventHandler = function(e)
{
  // if it is a right-click 
  if (e && e.getButton() == 2)
  {
    event.preventDefault(); // optional, depending on your requirements
    event.stopPropagation(); // optional, depending on your requirements
    openMenu(e.getScreenX(), e.getScreenY());
  }
}

Finally, the code for opening the custom menu would be something like the following.

// Insert a custom iframe at the x, y screen position 
var openMenu = function(x, y)
{
  var iframe = document.createElement('iframe');
  iframe.frameBorder = 0;
  iframe.scrolling = 'no';
  iframe.style.position = 'absolute';

  // build the menu as you require...
  // then position and show it.
  iframe.style.left = x + 'px'; 
  iframe.style.top = y + 'px'; // you may want to offset the position...

  document.body.appendChild(iframe ); // show the menu
}

Obviously what you put in the menu and how you style it is up to you. You would also probably want to hide it too, this would just be a case of removing the iframe - probably in another listener on the menu items (e.g. when you click a menu item the menu disappears)

If you get stuck here is a great reference for working with events. https://developers.google.com/earth/documentation/events

Also, here is a working example of the iframe shim technique: http://earth-api-samples.googlecode.com/svn/trunk/demos/customcontrols/index.html

Fraser
  • 15,275
  • 8
  • 53
  • 104
  • I found and used this technique before you answered, but your answer is almost exactly like my implementation. I didn't use the 'px' for the left and top, is that necessary? Thanks for the reply Fraser! – JGood Apr 03 '12 at 20:43
  • Good stuff...Yes, the the definitions for left and top are "auto|length|%|inherit". Expressly adding length units is a very good idea as it removes any ambiguity as to whether you intended pixels or percent. Some browsers require the units (FireFox) some will default to pixels (IE), others will do yet other things. If you intend pixels then use them. – Fraser Apr 08 '12 at 15:42
  • Doh! You're right!! I forgot completely about the pixel vs. percentages. Such a simple thing easily forgotten. Thanks once again. – JGood Apr 08 '12 at 19:56