How to sort results by distance with google places api using textsearch

yehudahs picture yehudahs · Jul 28, 2012 · Viewed 7.3k times · Source

I want to get all the restaurants near my location order by distance. I am using textsearch with google places api using this url : https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurants&location=32.16107,34.806618&radius=200&sensor=true&key=api_key. The results that I am getting are sparse with no order what so ever. I tried rankby=distance but accurding to the documentation (https://developers.google.com/places/documentation/) you can use it only with regular search,means that when using textsearch there is no such parameter option (I tried it anyway -> not working). I am using textsearch because regular search returns only "types" : [ "establishment" ]. How can I order by distance the textsearch of google place api ? I can't believe that google didn't create a way to do it...

Answer

Chris Green picture Chris Green · Jul 30, 2012

You are correct, Places API Textsearch does not support the rankBy=distance parameter. If you believe that this would be a useful feature, please submit a Places API Feature Request.

You can however obtain the result you are looking for by performing a Places API Search Request using the rankBy=distance parameter and the keyword=restaurant parameter:

https://maps.googleapis.com/maps/api/place/search/json?keyword=restaurant&location=32.16107,34.806618&rankBy=distance&sensor=false&key=YOUR_API_KEY

The keyword parameter is matched against all available fields, including but not limited to name, type, and address, as well as customer reviews and other third-party content.