点击虚拟地球信息框而不是悬停?

ben*_*ier 7 javascript virtual-earth

是否有可能使Virtual Earth图钉信息框显示响应来自onclick而不是鼠标悬停?

目前我正在使用这样的东西:

...

  var pin = new VEShape(
                  VEShapeType.Pushpin,
                  [location]
                );
  pin.SetCustomIcon(icon_url);
  pin.SetTitle(title);
  pin.SetDescription(info_window_template);
  map.AddShape(pin);

  document.getElementById(pin.GetPrimitive().iid).onmouseover = EventHandlerOnMouseOver;
}

var EventHandlerOnMouseOver = function(e) {
    // Handle content loading...
}
Run Code Online (Sandbox Code Playgroud)

...

但是,如果我尝试将onmouseover更改为onclick,则VE会选择onclick并完全禁用信息框.

Mar*_*son 1

您可以通过使用事件来完成所描述的内容...请注意,我使用的是 Virtual Earth 6.2。

诀窍是抑制 onmouseover 事件并订阅 onclick 事件。当您确定用户是否单击了某个形状后,您可以调用地图控件上的 ShowInfoBox 方法来强制其显示信息框。

这是代码 =)

// Begin by supressing the onmouseover event. Returning true 
// from an event handler disables the default Virtual Earth action
map.AttachEvent('onmouseover', function(e) { if(e.elementID) return true; });

// Subscribe to the onclick event and react whenever we get an element id
map.AttachEvent("onclick", function(e) {

    // elementID is null when someone clicks on the map and not on a shape
    if(e.elementID) {

        var shape = map.GetShapeByID(e.elementID); 
        if(shape) map.ShowInfoBox(shape);
    } 
});
Run Code Online (Sandbox Code Playgroud)

请注意,即使您右键单击形状,信息框也会显示;为了避免这种情况,您可以查看事件对象的 leftMouseButton 或 rightMouseButton 属性。

参考: