未捕获的TypeError:无法读取属性'添加' undefined :: javascript :: bing maps

时间:2016-07-25 19:02:46

标签: javascript bing-maps

所以我对这个displayinfobox功能有问题。我正在研究Bing Maps v8 API,试图将事件与信息框连接起来。我正在关注Bing并在信息框对象中存储事件处理程序。我得到这个阅读属性'添加'错误。

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

我正在尝试按此脚本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存在错误,或者只是更改了此代码的处理方式。我已将大部分代码复制到一个小提琴项目中,它在v7中运行良好。所以我要手动完成这个。

我将发布一次工作代码,这已经完成。

2 个答案:

答案 0 :(得分:1)

问题是Infobox类没有mouseleave或mouseenter事件。因此,当您尝试为此添加事件处理程序时,此步骤的“添加”部分将失败,因为这些事件不存在。

答案 1 :(得分:0)