2012-03-28 78 views
1

我一直在使用Google地球插件来构建特定的功能,我想要做的事情之一是通过GE创建我自己的上下文菜单。Google地球插件上下文菜单

有没有人这样做?任何帮助是极大的赞赏。

回答

1

您可以通过侦听mouseup或click事件,然后使用填充叠加技术来显示自定义上下文菜单来实现此目的。

事件侦听器的代码将如下所示。

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

事件处理程序将类似于以下内容。

// 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()); 
    } 
} 

最后,打开自定义菜单的代码将如下所示。

// 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 
} 

很明显,您在菜单中放入的内容以及样式取决于您。你也可能想要隐藏它,这只是一个删除iframe的情况 - 可能在另一个菜单项上的监听器(例如,当你点击菜单项时菜单消失)

如果你在这里被卡住是与事件合作的重要参考。 https://developers.google.com/earth/documentation/events

而且,这里是iframe的垫片技术的工作示例: http://earth-api-samples.googlecode.com/svn/trunk/demos/customcontrols/index.html

+0

我发现你回答之前使用这种技术,但你的回答是几乎完全一样我的实现。我没有使用左侧和顶部的'px',这是必要的吗?感谢您的答复弗雷泽! – goodwince 2012-04-03 20:43:56

+0

好东西......是的,左边和上边的定义是“自动|长度|%|继承”。明确添加长度单位是一个非常好的主意,因为它消除了您是否想要像素或百分比的任何歧义。一些浏览器需要单位(FireFox),一些会默认为像素(IE),其他的则会做其他事情。如果你打算像素,然后使用它们。 – Fraser 2012-04-08 15:42:18

+0

Doh!你是对的!!我完全忘记了像素与百分比。这种简单的事情很容易被人遗忘。再次感谢。 – goodwince 2012-04-08 19:56:26