最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can we restrict google map autocomplete to only a particular city? - Stack Overflow

programmeradmin0浏览0评论

My requirement is to get google places autoplete 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,
            ponentRestrictions: {country: "in"}
           };
var autoplete = new google.maps.places.Autoplete(input, options);

autoplete.bindTo('bounds', map);

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

  //rest_of_the_code

});

Please somebody help !!

My requirement is to get google places autoplete 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,
            ponentRestrictions: {country: "in"}
           };
var autoplete = new google.maps.places.Autoplete(input, options);

autoplete.bindTo('bounds', map);

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

  //rest_of_the_code

});

Please somebody help !!

Share Improve this question edited Nov 28, 2016 at 11:41 sajalsuraj asked Nov 28, 2016 at 11:22 sajalsurajsajalsuraj 99217 silver badges34 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

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 autoplete = new google.maps.places.Autoplete(this, {
   bounds: bangaloreBounds,
   strictBounds: true,
});

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

});

Note: The URL should be: https://maps.googleapis./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.

I think that you can try this:

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'],
  ponentRestrictions: {country: 'est'}
};

Replace LatLng value with your values and country with your country code.

You can use bounds to bias results, but there is no strict filter by city in places autoplete at the moment.

You can find a feature request to add more strict filters in the public issue tracker:

https://code.google./p/gmaps-api-issues/issues/detail?id=4433

Please star this feature request to express your interest and receive updates from Google.

Google is not providing autoplete for particular city but we can do it manually. in my case i want to display address of 'Maharashtra' state.

  this.GoogleAutoplete = new google.maps.places.AutopleteService();

    this.GoogleAutoplete.getPlacePredictions(
  {
    input: this.autopleteFrom.input,
    types: ['geocode'],
    ponentRestrictions: { country: 'in' },
  },
  (predictions, status) => {
    this.autopleteItemsFrom = [];
    this.zone.run(() => {
      if (predictions) {
        console.log(predictions)
        predictions.forEach((prediction) => {
          var statearray = prediction.description.split(",")
          var state = statearray[statearray.length - 2]
          if(state === ' Maharashtra'){
            this.autopleteItemsFrom.push(prediction);
          }
        });
      }
    });
  });

i hope it will help you.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>