2016-11-28 80 views
0

我的要求是隻爲在班加羅爾的地方獲得谷歌地方的自動填充建議,但如果我搜索除了班加羅爾以外的任何其他地方,那麼我也會得到我不想要的建議。我發現了很多與我的問題相關的計算器鏈接,但他們都沒有解決我的問題。我們如何才能限制谷歌地圖自動完成只有一個特定的城市?

這是可能得到任何特定城市的建議?

我下面分享我的代碼: -

var input = (document.getElementById('address')); 
      var s_w = new google.maps.LatLng(12.97232, 77.59480); //southwest 
      var n_e = new google.maps.LatLng(12.89201, 77.58905); //northeast 
      var b_bounds = new google.maps.LatLngBounds(s_w, n_e); //bangalorebounds 

var options = { 
      bounds:b_bounds, 
      componentRestrictions: {country: "in"} 
      }; 
var autocomplete = new google.maps.places.Autocomplete(input, options); 

autocomplete.bindTo('bounds', map); 

autocomplete.addListener('place_changed', function() { 

    //rest_of_the_code 

}); 

請人幫助!

回答

1

我認爲,你可以試試這個:

var cityBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(25.341233, 68.289986), 
    new google.maps.LatLng(25.450715, 68.428345)); 

var options = { 
    bounds: cityBounds, 
    types: ['geocode'], 
    componentRestrictions: {country: 'est'} 
}; 

更換LatLng值與你的價值觀和country與貴國的代碼。

+0

這也給其他城市的建議 – sajalsuraj

+0

呃......根據https://developers.google.com/maps/documentation/javascript/reference#AutocompletionRequest,你可以使用'bounds'和'componentRestriction'。另一種方法是使用'location',根據您在API中使用的位置給出建議。 –

+0

好的,我會嘗試與位置替代 – sajalsuraj

3

試試這個。

var bangaloreBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(12.864162, 77.438610), 
    new google.maps.LatLng(13.139807, 77.711895)); 

var autocomplete = new google.maps.places.Autocomplete(this, { 
    bounds: bangaloreBounds, 
    strictBounds: true, 
}); 

autocomplete.addListener('place_changed', function() { 

}); 

注:URL應該是:https://maps.googleapis.com/maps/api/js?libraries=places&key=YOUR_API_KEY

strictBounds option在地圖的JavaScript API這是目前的3.27版本中添加(一月2017)實驗版本。

相關問題