2017-07-26 32 views
-1

我很努力讓Google地方信息查找和地理編碼在同一頁面中一起工作。我認爲我如何加載這兩個API存在衝突。爲Google地方信息和地理編碼包含哪些腳本

我用這一個加載的地方API

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&key=*****************&callback=initAutocomplete&types=address" async defer></script> 

地方查找與工作正常。

地理編碼,這是第一行代碼,我試圖讓工作

geocoder = new google.maps.Geocoder(); 

和它的作品(當然,不拋出一個錯誤),如果我有這個在我的網頁:

<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> 

但是,這幾乎是我已經有的相同的URL,並且包括它打破了地方查找。當我同時包含這兩個腳本時,我也會收到一條警告,告知我包含多次可能會導致問題的Maps API。

請問有人能告訴我哪些腳本包含在一起工作嗎?在這一點上,我發現文檔很混亂,說實話。

回答

2

它應該只是你的第一個包括工作正常。它包含基礎Google Maps API 加上 Places庫。

var initMap = function() { 
 
    var geocoder = new google.maps.Geocoder(); 
 
    geocoder.geocode({ 
 
    address: '10 Downing St, Westminster, London SW1A 2AA, UK', 
 
    }, function (results, status) { 
 
    if (status === 'OK') { 
 
     var result = results[0]; 
 
     alert('latitude: ' + result.geometry.location.lat()); 
 
    } 
 
    else { 
 
     alert('Geocode was not successful for the following reason: ' + status); 
 
    } 
 
    }); 
 
};
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=initMap" async defer></script>

+0

感謝馬騰,我已經上了包括對鄰居功能的回調,所以我修改了包括什麼,我覺得應該爲我工作: – Richard

+0

包涵,我與SO編輯器有一個噩夢:) – Richard

+0