2016-12-05 54 views
0

我正在使用笨maps.googleapis.com/maps/api顯示自動完成位置,當用戶鍵入拉鍊地址特定國家的郵政編碼。我從第一個用戶的兩個輸入字段選擇國家和第二提起他會鍵入他的郵政編碼,並從谷歌自動完成,他將選擇他的地址。獲取使用谷歌地圖

var directionsService = new google.maps.DirectionsService(); 
     google.maps.event.addDomListener(window, 'load', function() { 
     new google.maps.places.SearchBox(document.getElementById('zip')); 
     directionsDisplay = new google.maps.DirectionsRenderer({ 'draggable': true }); 
    }); 

它工作正常,但現在我想只顯示選定國家的郵政編碼。也就是說,如果用戶選擇澳大利亞作爲國家和型拉鍊像2127那麼谷歌的API應該顯示在地址

回答

3

有2127澳大利亞地址谷歌地圖JS API提供了兩種類型的小部件的自動完成。一個是google.maps.places.SearchBox您正在使用,這隻能通過經度和緯度範圍提供限制,再有就是google.maps.places.Autocomplete提供國家限制(​​以及更多),例如。像這樣:

var input = document.getElementById('searchTextField'); 
var options = { 
    componentRestrictions: {country: 'au'} 
}; 

autocomplete = new google.maps.places.Autocomplete(input, options); 

有關的優勢更多信息/這兩種方法的缺點看docs(檢查節「類摘要」),並選擇適合您需求的最佳方法。

+1

您還可以添加類型:「地址解析」進入選項,以避免街道級地址和場所。 – miguev