0

我试图用get脚本调用google map api,然后用getscript调用bmap脚本,然后将bmap函数添加到同一个脚本中。这就是我试图调用谷歌地图 api 和 bmapsc 的方式

$.getScript("http://maps.google.co.uk/maps?    
file=api&v=2&async=2&callback=placeMap&key=my_API_key");
$.getScript("http://www.klossal.com/js/bmap/jQuery.bMap.1.3.js");
$("#map").bMap({
    mapZoom: 11,
    mapCenter:[51.501690392607,-0.1263427734375],
    mapSidebar:"sideBar", //id of the div to use as the sidebar
    markers:{"data":[
        {

"lat":51.49757618329838,"lng":-0.1746654510498047,"title":"Science  
Museum","body":"Exhibition Road, London SW7"
        },{

 "lat":51.47769451182406,"lng":-0.0009441375732421875,"title":"Royal Observatory 
Greenwich","body":"Blackheath Ave, Greenwich, London SE10"
        },{

"lat":51.49624032118747,"lng":-0.10857582092285156,"title":"Imperial War 
Museum","body":"Lambeth, London, SE1"
        },{

"lat":51.51792987720294,"lng":-0.1272869110107422,"title":"British Museum"                  
        },{

"lat":51.495625811469374,"lng":-0.17642498016357422,"title":"Natural History 
Museum","body":"Cromwell Road, London, SW7"                 
        },{

"lat":51.50177053585362,"lng":-0.12908935546875,"title":"Churchill Museum"                  
        }
    ]}
});
});

我不确定为什么这不起作用,对此的任何帮助都会很棒。

4

1 回答 1

0

我认为您在 getScript 中使用的 google maps API 的链接是错误的,不是吗?不应该

http://maps.google.co.uk/maps/api/js?sensor=false

代替

$.getScript("http://maps.google.co.uk/maps?file=api&v=2&async=2&callback=placeMap&key=my_API_key");

您不再需要 get 来使其工作,只有当您想监控地图使用情况时,您才能在此处看到

谷歌地图 JavaScript API v3

Google Maps JavaScript API v3 不需要 API 密钥即可正常运行。但是,我们强烈建议您使用 API 控制台密钥加载 Maps API,该密钥允许您监控应用程序的 Maps API 使用情况。了解如何使用 API 控制台密钥。

这是一种方法:

警告 UGLY 代码,仅用于回答钱包请使用 Require.js 或类似的东西在生产环境中进行这样的加载

$(document).ready(function(){
        $.getScript("http://maps.google.co.uk/maps/api/js?sensor=false",
            $.getScript("http://www.klossal.com/js/bmap/jQuery.bMap.1.3.js",
                function(){
                    $("#map").bMap({
                        mapZoom: 11,
                        mapCenter:[51.501690392607,-0.1263427734375],
                        mapSidebar:"sideBar", //id of the div to use as the sidebar
                        markers:{"data":[
                            {
                                "lat":51.49757618329838,"lng":-0.1746654510498047,"title":"Science Museum","body":"Exhibition Road, London SW7"
                            },{
                                "lat":51.47769451182406,"lng":-0.0009441375732421875,"title":"Royal Observatory Greenwich","body":"Blackheath Ave, Greenwich, London SE10"
                            },{
                                "lat":51.49624032118747,"lng":-0.10857582092285156,"title":"Imperial War Museum","body":"Lambeth, London, SE1"
                            },{
                                "lat":51.51792987720294,"lng":-0.1272869110107422,"title":"British Museum"                  
                            },{
                                "lat":51.495625811469374,"lng":-0.17642498016357422,"title":"Natural History Museum","body":"Cromwell Road, London, SW7"                    
                            },{
                                "lat":51.50177053585362,"lng":-0.12908935546875,"title":"Churchill Museum"                  
                            }
                        ]}
                    });
                }));
    });

这个回调应该有你的代码,所以它在 bMap 和 googleMaps 都加载后运行,这是因为 ajax 是异步的,所以它向 google 请求脚本,然后应用程序继续。

于 2012-10-16T17:23:56.197 回答