Google places autocomplete - location and radius n

2019-02-17 21:04发布

Im trying to set the google places autocomplete to sort the results from the nearest to a point. I have a code like this...

var defaultPlace = new google.maps.LatLng(49.227463, 16.546097);

var optionsAuto = {
    location: defaultPlace,
    radius: 20000,
    types: ['geocode'],
    componentRestrictions: {
        country: 'cz'
    }
};

var autocomplete = new google.maps.places.Autocomplete( inputStart, optionsAuto );

the types and componentRestrictions works great but the location and radius doesn't seems to be working.

3条回答
霸刀☆藐视天下
2楼-- · 2019-02-17 21:31

Look at the documentation

It states:

radius | Defines the distance (in meters) within which to return Place results. The maximum allowed radius is 50 000 meters. Note that radius must not be included if rankby=distance (described under Optional parameters below) is specified.

It sound like you want the optional parameter: rankby=distance:

rankby | distance. This option sorts results in ascending order by their distance from the specified location. Ranking results by distance will set a fixed search radius of 50km. One or more of keyword, name, or types is required.

Which is also available in the javascript service

Working Example

查看更多
Viruses.
3楼-- · 2019-02-17 21:50

to return places just in radius you need to set strictbounds to true.

The main problem for me right now is that Autocomplete doesn't have rankBy. see here

查看更多
smile是对你的礼貌
4楼-- · 2019-02-17 21:51

It's possible that someone may not be agree with me but In my case i defined the radius without quotes i.e like below

 var request = {
    location: pyrmont,
        bounds: map.getBounds(),
    radius: 2000,
    zoom: 15,
    types: ['restaurant']
  };

So it didn't make any difference on radius and showed the result independent of radius but when i applied quotes on it , radius parameter seems to be working

 var request = {
    location: pyrmont,
        bounds: map.getBounds(),
    radius: '2000',
    zoom: 15,
    types: ['restaurant']
  };
查看更多
登录 后发表回答