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

javascript - google maps autocomplete bounds not working - Stack Overflow

programmeradmin4浏览0评论

I want to use autoplete bounds on Google maps service. But not working.

//----autoplete start
var input = (document.getElementById('searchPlace'));
var defaultBounds = new google.maps.LatLngBounds(
            new google.maps.LatLng(40.518, 29.215),
            new google.maps.LatLng(41.242, 30.370));

var options = {
            bounds:defaultBounds,
            ponentRestrictions: { country: 'XX'},};

var autoplete = new google.maps.places.Autoplete(input, options);
//----autoplete end

When I type the textbox, the addresses are ing in XX country. But I wanna restrict the search in a region of country. For example a city bounds. But other addresses are ing out of this bounds. Bounds are not considered.

I want to use autoplete bounds on Google maps service. But not working.

//----autoplete start
var input = (document.getElementById('searchPlace'));
var defaultBounds = new google.maps.LatLngBounds(
            new google.maps.LatLng(40.518, 29.215),
            new google.maps.LatLng(41.242, 30.370));

var options = {
            bounds:defaultBounds,
            ponentRestrictions: { country: 'XX'},};

var autoplete = new google.maps.places.Autoplete(input, options);
//----autoplete end

When I type the textbox, the addresses are ing in XX country. But I wanna restrict the search in a region of country. For example a city bounds. But other addresses are ing out of this bounds. Bounds are not considered.

Share Improve this question edited Jul 6, 2017 at 5:03 ישו אוהב אותך 29.8k11 gold badges82 silver badges98 bronze badges asked Sep 20, 2013 at 6:55 bartelomabarteloma 6,87516 gold badges93 silver badges205 bronze badges 1
  • From the docs : The results are biased towards, but not restricted to, Places contained within these bounds. – Dr.Molle Commented Sep 20, 2013 at 9:13
Add a ment  | 

5 Answers 5

Reset to default 8

The problem is that LatLngBounds expects south-west and north-east corners. Also referred as bottom-left and top-right corners.

Instead of:

var defaultBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(40.518, 29.215),
    new google.maps.LatLng(41.242, 30.370));

It should be:

var defaultBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(41.242, 29.215),
    new google.maps.LatLng(40.518, 30.370));

Updated 10/2018: this answer is obsolete; please see other answers

It will not work as you wish. This is what documentation says:

The area in which to search for places. Results are biased towards, but not restricted to, places contained within these bounds.

https://developers.google./maps/documentation/javascript/places-autoplete#address_forms

var defaultBounds = new google.maps.LatLngBounds(
                new google.maps.LatLng(17.2916377 ,78.2387067),
                new google.maps.LatLng(17.5608321, 78.6223912)
);

var input = document.getElementById('destination');
var options = {
  bounds: defaultBounds,
  types: ['establishment'],
  strictBounds: true
};

var destination_autoplete = new google.maps.places.Autoplete(input, options)

Maybe when you are using bounds you cannot use ponentRestrictions...

Try this..

    //----autoplete start
    var input = (document.getElementById('searchPlace'));
    var defaultBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(40.518, 29.215),
        new google.maps.LatLng(41.242, 30.370));

    var options = {
        bounds:defaultBounds
    };
    var autoplete = new google.maps.places.Autoplete(input, options);
    //----autoplete end

Bounds only works for the maps API, not the Places API.

For location biasing in the Places API you need to use a location latlng object and a radius (in meters) in the options.

        var myLatlng = new google.maps.LatLng(35.9907,-84.0497);

        var acOptions = {
            location: myLatlng,
            radius: 150000,  // (in meters; this is 150Km)
            types: ['address'],
            ponentRestrictions: {country: 'us'}
        };

Note that this only biases the autoplete results to the radius you entered; it does not restrict the results to that radius.

发布评论

评论列表(0)

  1. 暂无评论