加载Bing地图的javascript文件后未定义 [英] Bing maps is undefined after loading their javascript file

查看:67
本文介绍了加载Bing地图的javascript文件后未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态加载 Bing Map API 脚本.脚本加载完成后,我想构建我的地图.问题是定义了MicrosoftMicrosoft.Maps,但未定义Microsoft.Maps.Map.我意识到他们的脚本将异步加载更多脚本,但是即使在等待10秒钟之后,这些附加的假设脚本仍未定义Microsoft.Maps.Map.那么,如何加载他们的Map类?在他们的示例中,没有看到任何内容明确加载了该类.

I am loading the Bing Map API script dynamically. When the script finishes loading, I would like to construct my map. The problem is Microsoft and Microsoft.Maps are defined, but Microsoft.Maps.Map is not. I realize that their script will asynchronously load more scripts, but even after waiting 10 seconds for these additional hypothetical scripts, Microsoft.Maps.Map continues to be undefined. So how do I load their Map class? I see nothing in their example, that explicitly loads the class.

Javascript(原型框架):

var script = new Element(
    'script', {
        type: 'text/javascript',
        src: 'http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0'
    }
);

script.observe(
    'load',
    function(event) {
        console.info(Microsoft);
        console.info(Microsoft.Maps);
        console.info(Microsoft.Maps.Map);
    }
);

document.body.appendChild(script);

控制台输出:

>>>  Microsoft
Object { Maps={...}}
>>>  Microsoft.Maps
Object { Globals={...}}
>>>  Microsoft.Maps.Map
undefined

推荐答案

cbayram是正确的,因为您查找得太早了.但具体来说,一旦脚本加载完成(onscriptload),您就不会使用Bing Map的特定方式来触发脚本.我们避免使用全局函数,但AFAIK确实需要在此处使用一个.

cbayram is right that you're looking too early. But specifically you are not using the Bing Map specific way of firing your script once theirs is done loading (onscriptload). We avoid global functions, but AFAIK you really need to use one here.

尝试一下:

var script = new Element(
    'script', {
        type: 'text/javascript',
        src: 'http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0&onscriptload=DrawMap'
    }
);

function DrawMap() {
   console.info(Microsoft);
   console.info(Microsoft.Maps);
   console.info(Microsoft.Maps.Map);
}

document.body.appendChild(script);

不重要,但是为什么要将它附加到身体上呢?我总是将其附加到头部.

Not important, but why do you append it to body? I always append it to head.

这篇关于加载Bing地图的javascript文件后未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆