我们如何将谷歌地图自动填充限制为仅限于特定城市?

saj*_*raj 0 javascript google-maps autocomplete google-maps-api-3

我的要求是获取谷歌地方自动完成建议只适用于班加罗尔的地方,但如果我搜索除了班加罗尔以外的任何其他地方,那么我也得到我不想要的建议.我找到了很多与我的问题相关的stackoverflow链接,但没有一个能解决我的问题.

这有可能得到任何特定城市的建议吗?

我在下面分享我的代码: -

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

});
Run Code Online (Sandbox Code Playgroud)

请有人帮忙!

San*_*San 5

试试这样吧.

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 () {

});
Run Code Online (Sandbox Code Playgroud)

注意:URL应为: https://maps.googleapis.com/maps/api/js?libraries=places&key=YOUR_API_KEY

在Maps JavaScript API版本3.27中添加了strictBounds选项,该版本目前(2017年1月)是实验版本.