简体   繁体   English

加载他们的javascript文件后,Bing地图未定义

[英]Bing maps is undefined after loading their javascript file

I am loading the Bing Map API script dynamically. 我正在动态加载Bing Map API脚本。 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. 问题是MicrosoftMicrosoft.Maps是定义的,但Microsoft.Maps.Map不是。 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. 我意识到他们的脚本会异步加载更多的脚本,但即使等待10秒后这些额外的假设脚本, Microsoft.Maps.Map仍然是未定义的。 So how do I load their Map class? 那么如何加载他们的Map类呢? I see nothing in their example , that explicitly loads the class. 我在他们的示例中看不到任何明确加载类的内容。

Javascript (Prototype Framework): 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);

Console Output: 控制台输出:

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

cbayram is right that you're looking too early. cbayram是对的,你看得太早。 But specifically you are not using the Bing Map specific way of firing your script once theirs is done loading (onscriptload). 但具体来说,一旦你的脚本完成加载(onscriptload),你就不会使用Bing Map特定的方法来触发你的脚本。 We avoid global functions, but AFAIK you really need to use one here. 我们避免全局功能,但AFAIK你真的需要在这里使用它。

Try this: 尝试这个:

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 Map API's JavaScript file dynamically loads/injects additional JS and CSS files into your page. BING Map API的JavaScript文件动态地将其他JS和CSS文件加载/注入到您的页面中。

<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/v7.0/7.0.20121012100453.93/js/en-us/veapicore.js">
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/v7.0/7.0.20121012100453.93/js/en-us/veapidelay.js">
<link rel="stylesheet" type="text/css" rev="stylesheet" href="http://ecn.dev.virtualearth.net/mapcontrol/v7.0/7.0.20121012100453.93/css/en/mapdelay.css">
<link rel="stylesheet" type="text/css" rev="stylesheet" href="http://ecn.dev.virtualearth.net/mapcontrol/v7.0/7.0.20121012100453.93/css/en/mapcontrol.css">
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/v7.0/7.0.20121012100453.93/js/en-us/veapiAnalytics.js"> 

The Map function/object seems to be created in Map函数/对象似乎是在创建的

http://ecn.dev.virtualearth.net/mapcontrol/v7.0/7.0.20121012100453.93/js/en-us/veapicore.js

EDIT: 编辑:

Your script.observe runs upon the first (initial) JS file being loaded. 您的script.observe在加载的第一个(初始)JS文件上运行。 It's too early for the Map function as it's instantiated in the veapicore.js that's subsequently loaded by the initial mapcontrol.ashx?v=7.0. 对于Map函数来说太早了,因为它在veapicore.js中实例化,随后由初始mapcontrol.ashx?v = 7.0加载。 You are simply too early in looking for that Map object. 您只是在寻找Map对象时太早。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM