调用谷歌地图和bmap脚本并添加功能

时间:2012-10-16 16:00:28

标签: jquery google-maps-api-3

我正在尝试使用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"                  
        }
    ]}
});
});

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

1 个答案:

答案 0 :(得分:0)

我认为你在getScript中使用谷歌地图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");

只有当您想监控地图使用情况you can see that here时,您才不再需要让它继续工作:

  

Google Maps JavaScript API v3

     

Google Maps JavaScript API v3不需要API密钥   功能正常。但是,我们强烈建议您加载   Maps API使用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然后应用程序继续运行。