2017-01-02 87 views
3

我試圖在同一頁面上包含兩個不同的Google API,但出現錯誤在同一頁面上有兩個不同的Google API,但可以獲得您在此頁面上多次包含Google Maps API錯誤

您已在此頁面上多次包含Google Maps API。

具體來說,我試圖使用谷歌的自動完成和距離矩陣API。我只能在單個頁面上工作,因爲我無法撥打maps.googleapis.com兩次。但是,我必須調用不同的鏈接擴展名(即callback=initAutocompletecallback=initMap),所以我不確定如何解決此問題。這兩個鏈接如下:

<script src="https://maps.googleapis.com/maps/api/js?key=MyPrivateKey=&libraries=places&callback=initAutocomplete" async defer></script> 

<script src="https://maps.googleapis.com/maps/api/js?key=MyPrivateKey4&callback=initMap" async defer></script> 

有誰知道如何通過一個腳本調用訪問自動完成和Map API?謝謝!

回答

1

通常,callback函數在Google地圖(如果請求加載所有庫)完全加載並可以使用時調用。因此,你可以把initAutoCompleteinitMap

<script> 
    var map; 
    function initMap(){ 
     // initiate a new map instance. 
     map = new google.maps.Map(document.getElementById('map'), { 
      zoom: 12, 
      center: {lat: 15.3647, lng: 75.1240} 
     }); 
     initAutocomplete(); // initiate Auto complete instance 
    } 
    fucntion initAutocomplete(){ 
     // code to handle autocomplete 
    } 
</script> 
<script src="https://maps.googleapis.com/maps/api/js?key=<key>&libraries=places&callback=initMap" async defer></script> 
+0

謝謝!!!!這工作很好 - 除了我爲initAutocomplete執行了回調,然後在自動完成地址中使用了Autocomplete中的initMap。你爲我節省了許多小時的挫折。乾杯,喬 – Joe123

相關問題