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

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

问题描述

我的要求是仅针对班加罗尔的地点获得谷歌地点自动完成建议,但如果我搜索班加罗尔以外的任何其他地点,那么我也会得到我不想要的建议.我发现了许多与我的问题相关的 stackoverflow 链接,但没有一个能解决我的问题.

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

});

请有人帮忙!!

推荐答案

试试这个.

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 选项 已在 3.27 版中添加Maps JavaScript API 目前(2017 年 1 月)的实验版本.

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

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

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