Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google places autocomplete - location and radius not working

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.

like image 636
adam Avatar asked Aug 04 '12 17:08

adam


People also ask

How do I restrict Google Maps autocomplete to certain cities?

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.

Is google places autocomplete free?

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.


1 Answers

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

like image 160
geocodezip Avatar answered Sep 27 '22 19:09

geocodezip