Uncaught TypeError: Cannot read property 'add' of undefined :: javascript :: bing maps

Uncaught TypeError: Cannot read property 'add' of undefined :: javascript :: bing maps

所以我在使用此显示信息框功能时遇到了问题。我正在研究 Bing Maps v8 API 试图 link 使用信息框添加事件。我正在关注 Bing 并将事件处理程序存储在信息框对象中。我正在读 属性 'add' 错误。

在堆栈跟踪中,它会将我带到我添加事件处理程序的那一行,或者当我检查信息框是否附加了任何事件时。

我正在尝试在此处遵循此脚本 https://blueric.wordpress.com/2011/03/10/creating-hover-style-info-boxes-on-the-bing-maps-ajax-v7-0-control/。鉴于它已经很老了,我认为事情可能会以类似的方式工作。

已编辑:

好像跟addHandler有点关系,我搞混了。

 function displayInfoBox(e) {

    stopInfoBoxTimer(e);

    console.log("after Infobox Timer");

    if (_displayInfoType != "INFOBOX")
        return;

    var replaceContent = infoboxTemplate;
    var ticket = e.target.ticket;
    var contentHead = "<ul>";
    var contentEnd = "</ul>";
    var content = "";
    content += "<li><b>:</b> " + ticket.{REMOVED VAR} + "</li>";
    content += "<li><b>:</b> " + ticket.{REMOVED VAR} + "</li>";
    content += "<li><b>:</b> " + ticket.{REMOVED VAR} + "</li>";
    content += "<li><b>:</b> " + ticket.{REMOVED VAR} + "</li>";
    content += "<li><b>:</b> " + ticket.{REMOVED VAR}+ "</li>";
    content += "<li><b>:</b> " + ticket.{REMOVED VAR}+ "</li>";
    content += "<li><b>:</b> " + ticket.{REMOVED VAR}+ "</li>";

    if (ticket.WW_PK != "")
        content += "<li><b>:</b> " + ticket.{REMOVED VAR}+ ", " + ticket.{REMOVED VAR}+ "</li>";
    else
        content += "<li><b>: <span  style='color:red;'>{REMOVED VAR}</span></b></li>";


    replaceContent = replaceContent.replace('{title}', ticket.{REMOVED VAR});
    replaceContent = replaceContent.replace('{content}', contentHead + content + contentEnd);

    console.log("after replaced content");
    var options = {
        offset: new Microsoft.Maps.Point(23, 173),
        location: e.target.getLocation(),
        htmlContent: replaceContent,
        visible: true
    };

    console.log(options);

    console.log("before removing events from infobox");
    if (infobox != null && infobox.mouseLeaveHandler != null && infobox.mouseEnterHandler != null) {

        if (Microsoft.Maps.Events.hasHandler(infobox, 'mouseleave'))
            Microsoft.Maps.Events.removeHandler(infobox.mouseLeaveHandler);

        if (Microsoft.Maps.Events.hasHandler(infobox, 'mouseenter'))
            Microsoft.Maps.Events.removeHandler(infobox.mouseEnterHandler);

        infobox = null;
    }
    console.log("after removing events from infobox");
    console.log(infobox);
    infobox = new Microsoft.Maps.Infobox(location, options);
//-------------------------- ERROR IN HERE ---------------------------------

    infobox.mouseLeaveHandler = Microsoft.Maps.Events.addHandler(infobox, 'mouseleave', infoBoxMouseLeave);
    infobox.mouseEnterHandler = Microsoft.Maps.Events.addHandler(infobox, 'mouseenter', infoBoxMouseEnter);
//-------------------------- ERROR IN HERE ---------------------------------
    console.log(infobox.mouseEnterHandler);
    infobox.setMap(map);
 }

已编辑(最新): Bing V8 似乎存在错误,或者只是处理此代码的方式发生了变化。我已将大部分代码复制到 fiddle 项目中,它在 v7 中运行良好。所以我将手动执行此操作。

我会post一次工作代码,这就完成了。

Microsoft 在 v8 中禁用了信息框上的事件。

https://social.msdn.microsoft.com/Forums/en-US/31fdf1ce-d0e2-47d2-9d34-5d722cccd291/why-are-infobox-events-not-needed-in-v8?forum=bingmapsajax

问题是信息框 class 没有 mouseleave 或 mouseenter 事件。因此,当您尝试为此添加事件处理程序时,此步骤的 "add" 部分失败,因为这些事件不存在。