在谷歌地图中如何限制自动搜索印度的单个城市

时间:2018-10-17 04:57:52

标签: javascript jquery google-maps

在国家/地区,印度仅对浦那市限制自动搜索。我们如何实现这一目标?

我尝试过

autocomplete.setComponentRestrictions(
      {'country': ['in']},{'city':['Pune','Mumbai']});

2 个答案:

答案 0 :(得分:1)

尝试这可能会有所帮助

post

或者您可以尝试

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

更有效的方式自己完成

var input = document.getElementById('searchCity');
var options = { types: ['(cities)'],componentRestrictions: {country: 'us' }};
var autoComplete = new google.maps.places.Autocomplete(cityInput,options);`

更多访问here

答案 1 :(得分:1)

您可以这样做:

function initMap() {
  var geocoder = new google.maps.Geocoder;
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 8,
    center: {
      lat: 12.4716,
      lng: 77.588
    }
  });

  document.getElementById('submit').addEventListener('click', function() {
    geocodeAddress(geocoder, map);
  });
}

function geocodeAddress(geocoder, map) {
  geocoder.geocode({
    componentRestrictions: {
      country: 'IND',
      postalCode: '56000'
    }
  }, function(results, status) {
    if (status === 'OK') {
      map.setCenter(results[0].geometry.location);
      new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
      });
    } else {
      window.alert('Geocode was not successful for the following reason: ' +
        status);
    }
  });
}