2012-10-16 91 views
0

我試圖調用谷歌地圖api與獲取腳本,然後通過調用bmap腳本也與getscript,然後添加到相同的腳本bmap函數。這是我試圖調用谷歌地圖API和bmapsc調用谷歌地圖和bmap腳本並添加功能

$.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"     
     } 
    ]} 
}); 
}); 

我不知道這是爲什麼不工作,有這方面的幫助將是巨大的。

+0

控制檯上的任何錯誤我們應該注意到? –

+0

當我用jquery加載腳本時,頁面顯示正常,我用jquery做了一些事情來打破它。 – loriensleafs

+0

http://www.klossal.com/media/map.html在此處運行良好,但在我使用的其他站點中,我無法以此方式運行。 – loriensleafs

回答

0

我認爲你在getScript中使用的google地圖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,使其工作了,只要你想監控的地圖使用you can see that here

Google Maps JavaScript API v3

Google Maps JavaScript API v3不需要API密鑰 函數正確。不過,我們強烈建議您使用API​​控制檯密鑰加載 Maps API,以便您監控應用的Maps API使用情況。瞭解如何使用API​​控制檯密鑰。

這裏是一個辦法做到這一點:

警告醜陋的代碼,只是爲了回答pursoses請使用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是異步的,所以它請求腳本到谷歌,然後應用程序繼續。

+0

嗯,api鑰匙去哪了? – loriensleafs

+0

我也看過這個,但可以弄清楚如何將這個關鍵字合併到http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=SET_TO_TRUE_OR_FALSE – loriensleafs

+0

已更新我的答案以添加解釋那。 –