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.
It is currently not possible to restrict results to a specific locality. You can use bounds as you have done so above to bias results towards, but not restricted to places contained within the specified bounds. If you believe restriction by locality would be a useful feature please file a Places API - Feature Request.
The autocomplete request is available at no charge, and the subsequent Place Details call gets charged based on regular Place Details pricing. A Place Details request generates Data SKUs (Basic, Contact, and/or Atmosphere) – depending on the fields that are specified in the request.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With