【问题标题】:Trigger Click Event in Bing Maps在 Bing 地图中触发点击事件
【发布时间】:2015-06-09 19:10:30
【问题描述】:

Bing 地图上的点击事件有点麻烦。 我有很多商店,我可以毫无问题地放置所有 pin 和 click 事件(打开信息框)

var STORES = [
    {
        id: 123,
        lat: 1.23456789,
        lng: -1.23456789,
        name: 'AEVA'
    },
    ...
]

for (var i = 0; i < STORES.length; i++) {
    var pinOptions: {icon: 'map-pin.png?id'+STORES[i].id, width: 29, height: 52},
        LatLng = new Microsoft.Maps.Location(STORES[i].lat, STORES[i].lng),
        pin = new Microsoft.Maps.Pushpin(LatLng, pinOptions);

    pin.content = '<p>'+STORES[i].name+'</p>';

    Microsoft.Maps.Events.addHandler(pin, 'click', displayInfobox);

    bing.pinLayer.push(pin);
}

现在的问题是,当用户通过搜索页面到达该页面时,基本上会添加一个带有 id 的标签,例如 /stores#id123

我希望地图自动打开带有该 ID 的框,所以我添加了此代码

var hash = window.location.hash;
hash = hash.replace("#", "");
if(hash.length>0){
    var pin = $('img[src*="?'+hash+'"]').parent();
    pin.trigger('click');
}

但它只是行不通。我也试过了

Microsoft.Maps.Events.invoke(a, 'click');

但是什么都没发生,有没有人有什么解决方法,触发点击事件?

谢谢

【问题讨论】:

  • 哈希是/storesid123吗?
  • 另外,“它只是行不通”是什么意思?您能否更具描述性或喜欢弹出哪些错误?
  • 当我说“它不起作用”时,我的意思是“触发器('click')”不会运行打开框“Microsoft.Maps.Events.addHandler(pin , '点击', 显示信息框);"
  • 问题不在于哈希,而在于 bing 使用的事件处理程序。我无法访问它(不知道如何触发它)。

标签: jquery bing-maps


【解决方案1】:

Microsoft.Maps.Events.invoke 函数需要 实体对象 而不是 Html 元素。实体可以是以下任何一种类型:InfoboxPolygonPolylinePushpinTileLayerEntityCollection

话虽如此,您可以考虑以下方法来查找Pushpin

function findPin(map,storeId)
{
    for(var i = 0; i < map.entities.getLength();i++){
        var entity = map.entities.get(i);
        if(entity.getIcon === undefined) continue;
        var icon = entity.getIcon();  
        if(entity.getIcon() === "map-pin.png?id" + storeId) return entity;
    }
    return null;
}

用法

var storeId = window.location.hash.replace("#id", "");
if(storeId.length > 0){
    var selectedPin = findPin(bing,storeId);
    Microsoft.Maps.Events.invoke(selectedPin, 'click');
}

在哪里

function displayInfobox(e) {
    infobox.setLocation(this.target.getLocation());
    infobox.setOptions({ visible: true });
}

【讨论】:

  • 这是正确的。去年我在创建 Bing Maps 热图应用程序时遇到了同样的问题,这就是我解决问题的方法。
  • 我不得不稍微调整一下,但它奏效了,谢谢
  • 我在尝试使用未定义目标的 e.target 时遇到问题。使用“this”解决了我的问题。谢谢你的好样品。
猜你喜欢
  • 1970-01-01
  • 2013-04-05
  • 2023-03-27
  • 1970-01-01
  • 2015-02-06
  • 1970-01-01
  • 1970-01-01
  • 2015-04-28
  • 2011-11-19
相关资源
最近更新 更多