2012-07-02 79 views
2

此代碼無效。請告訴我確切的解決方案將Google自動填充列表中的國家/地區的Google地圖限制爲「印度,美國和英國」

<script src="maps.googleapis.com/maps/api/…; type="text/javascript"></script> 
<script type="text/javascript"> 
    function initialize() { 
     var input = document.getElementById('searchTextField'); 
     /* restrict to multiple cities? */ 
     var options = { 
      types: ['(cities)'], 
      componentRestrictions: {country: ["usa", "uk"]} 
     }; 
     var autocomplete = new google.maps.places.Autocomplete(input, options); 
    } 
    google.maps.event.addDomListener(window, 'load', initialize); 
</script> 
+2

它必須是確切的解決方案! – elclanrs

+0

它不起作用 –

+0

@elclanrs Lmao:P,OP - 什麼是自動完成和谷歌地圖之間的關係詳細說明你的問題更多bru! –

回答

6

首先,你不能從多個國家過濾。這是一個已知問題reported here

那麼對於你的代碼,你必須使用一個兩個字符的字符串爲國家代碼:

componentRestrictions可用於結果限制爲特定 組。目前,您可以使用componentRestrictions通過 國家進行過濾。該國必須通過兩個字符,ISO 3166-1 Alpha-2兼容的國家代碼。 Source

這裏是一個國家working sample我jsfiddled(取自Google samples list)。

-2

爲什麼你不把單選按鈕/複選框等獲取多個國家地址也許這將爲你們工作。 var valueofcountry =「ca」; //想這值從複選框來/收音機

var autoCompleteOptions = {componentRestrictions: {country: valueofcountry}}; 
-2

最終我得到了這個問題..我希望這個代碼將work..try這

// FOR SPECIFIC COUNTRY 'INDIA' ONLY 
var options = { 
    types: ['(cities)'], 
    componentRestrictions: {country: "IN"} 
}; 

autocomplete = new google.maps.places.Autocomplete(document.getElementById('text_box_id'), options); 
/* When the user selects an address from the dropdown, 
    populate the address fields in the form. */ 
google.maps.event.addListener(autocomplete, 'place_changed', function() { 
    fillInAddress(); 
}); 

// FOR SPECIFIC COUNTRY 'US' ONLY 
var options = { 
    types: ['(cities)'], 
    componentRestrictions: {country: "US"} 
}; 

autocomplete = new google.maps.places.Autocomplete(document.getElementById('text_box_id'), options); 
/* When the user selects an address from the dropdown, 
    populate the address fields in the form. */ 
google.maps.event.addListener(autocomplete, 'place_changed', function() { 
    fillInAddress(); 
}); 
2

等待兩年後,一個解決方案來自谷歌的解決方案,我已經意識到一個新項目的解決方法。

該函數正在過濾多個組件限制,並將頂部結果合併到一個數組。這意味着特定的結果集會逐一給出。

也許這個解決方案可以被優化,即所有國家或地區的整體結果應該用於行政區域,城市和街道的「重量」。

撥弄的腳本是here

主要功能(具有回調的多個請求後):

 service = new google.maps.places.AutocompleteService(); 
     var request1 = { 
     input: inputData, 
     componentRestrictions: {country: 'de'}, 
     }; 
     var request2 = { 
     input: inputData, 
     componentRestrictions: {country: 'at'}, 
     }; 
     var request3 = { 
     input: inputData, 
     componentRestrictions: {country: 'ch'}, 
     }; 
     $('#result').empty(); 
     service.getPlacePredictions(request1, callback); 
     service.getPlacePredictions(request2, callback);    
     service.getPlacePredictions(request3, callback); 
4

這裏的身邊我一直在使用通過操縱多個自動完成的div的位置的工作:

var location = $('#location'); 
var options1 = { 
        types: ['(cities)'], 
        componentRestrictions: {country: 'uk'} 
       } 
var options2 = { 
        types: ['(cities)'], 
        componentRestrictions: {country: 'irl'} 
       } 
if (location.exists()) { 
    $(location).each(function(index){ 
     new google.maps.places.Autocomplete(this, options2); 
     new google.maps.places.Autocomplete(this, options1); 
    }); 
var pressed = 0; 
      google.maps.event.addDomListener(this, 'keydown', function(e){ 
      if (e.keyCode == 40 || e.keyCode == 38) { 
       e.preventDefault(); 
       e.stopPropagation(); 
       e.stopImmediatePropagation(); 
      } 
     },true); 
    location.keypress(function(e) { 
     setTimeout(function(e){ 
      setInterval(function(e){ 
      if($('.pac-container.pac-logo').last().css('display') != 'none'){ 
       $('.pac-container.pac-logo').first().children('.pac-item').appendTo($('.pac-container.pac-logo').last()) 
      } 
      else{ 
       $('.pac-container.pac-logo').last().children('.pac-item').appendTo($('.pac-container.pac-logo').first()) 
      } 
      },100) 

     } 
     ,300) 
    }); 
} 

CSS:

.pac-container.pac-logo:last-of-type { 
    box-shadow: none !important; 
} 
.pac-container.pac-logo:last-of-type:after { 
    content: none !important; 
} 
+1

直到谷歌決定花費1分鐘的開發更新,這是最好的,你可以得到 –

1

Just to update,版本3.27發佈後o f 2017年1月發佈Maps JavaScript API時,多個國家/地區的過濾器可能會傳遞一組字符串國家/地區代碼。

相關問題