我们如何才能将Google地图自动完成功能限制为仅限特定城市? [英] How can we restrict google map autocomplete to only a particular city?

查看:143
本文介绍了我们如何才能将Google地图自动完成功能限制为仅限特定城市?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是只为班加罗尔的地方提供Google地方的自动填充建议,但如果我搜索除了班加罗尔以外的任何其他地方,我也会收到我不想要的建议。我发现了很多与我的问题相关的计算器链接,但他们都没有解决我的问题。



是否有可能获得任何特定城市的建议?



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

  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 = {
界限: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

});

请别人帮忙!!

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

});

注意:该网址应该是: https://maps.googleapis。 com / maps / api / js?libraries = places& key = YOUR_API_KEY



strictBounds选项已添加到Maps JavaScript API的版本3.27中,该版本目前是(2017年1月)的实验版本。

My requirement is to get google places autocomplete suggestion only for the places in Bangalore, but if I search for any other place apart from bangalore then also I get the suggestions which I don't want. I have found many stackoverflow links related to my question but none of them are solving my problem.

Is this possible to get the suggestions of any particular city ?

I am sharing my code below :-

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

});

Please somebody help !!

解决方案

Try like this.

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

});

Note: The URL should be: https://maps.googleapis.com/maps/api/js?libraries=places&key=YOUR_API_KEY

strictBounds option was added in version 3.27 of Maps JavaScript API which is currently (January 2017) the experimental version.

这篇关于我们如何才能将Google地图自动完成功能限制为仅限特定城市?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆