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

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...

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.

Related

How to search for locations using Tags inside OpenStreetMap

Using the Nominatim APIs, am able to do a search based on a query. However, all the results returned have a Tag associated with them, like leisure=fitness_centre.
How can I, thus, use the OpenStreetMap APIs to search a given location (say London) for all the fitness_centre tags? Idea is to be able to search the map, zero in a location (say London, Oxford etc.) and then search for all places/locations with Tag [leisure=fitness_centre]
Using Google Places API, one can search using tags using something like below (specifying type=gym) -
https://maps.googleapis.com/maps/api/place/search/json?location=51.509865,-0.118092&radius=50000&sensor=true&key=&types=gym
Only issue with the Google Places API is that they are extremely pricey!!, so looking for OpenStreetMap as an alternative.
For searching specific objects in OSM, Overpass API is usually a good choice.
There is a nice frontend available called overpass turbo. Open the wizard, enter "leisure=fitness_centre in London" and it will generate an appropriate query:
[out:json][timeout:25];
{{geocodeArea:London}}->.searchArea;
(
node["leisure"="fitness_centre"](area.searchArea);
way["leisure"="fitness_centre"](area.searchArea);
relation["leisure"="fitness_centre"](area.searchArea);
);
out body;
>;
out skel qt;
Then hit run to see the results on the map: https://overpass-turbo.eu/s/1fbg
Please note that the {{geocodeArea:London}}->.searchArea; part is a feature added by the overpass turbo frontend. Overpass API won't understand this geocoding request. Instead, you have to replace it with an area query and the corresponding relation ID (in this case area(id:3600065606)->.searchArea;).
Another thing to note is that Overpass API servers shouldn't be used for heavy queries or commercial purposes. If you need this kind of service then install your own Overpass API instance or use a commercial Overpass API provider.

Find Place requests Returns Only One Result

I'm using the Google Places API endpoint "findplacefromtext" and tried a search similar to the example.
https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=mongolian%20grill&inputtype=textquery&fields=photos,formatted_address,name,opening_hours,rating&locationbias=circle:2000#47.6918452,-122.2226413&key=YOUR_API_KEY
However, when you use this it only ever returns one result. There is a cafe near me that's called "Cream" but when you pass that as the "input" parameter it returns shops that have a category of "Ice Cream". I thought it should only search the name of the business.... If I can't find the place by name does it search the category type as a fall back? When I execute the same search in Google Maps it returns the same data but I get multiple results and I can see the place I am trying to retrieve 3rd on the search result list.
Is it possible to make it return more than one result? The documentation doesn't mention anything about this.
I believe what you need is the Text Search request. The Find Place request is meant for exact addresses.
The Google Places API Text Search Service is a web service that
returns information about a set of places based on a string — for
example "pizza in New York" or "shoe stores near Ottawa" or "123 Main
Street". The service responds with a list of places matching the text
string and any location bias that has been set.
The service is especially useful for making ambiguous address queries
in an automated system, and non-address components of the string may
match businesses as well as addresses. Examples of ambiguous address
queries are incomplete addresses, poorly formatted addresses, or a
request that includes non-address components such as business names.
Taken from https://developers.google.com/maps/documentation/places/web-service/search#TextSearchRequests

How do I restriction a Google Places Search to a specific country?

The documentation says how I can limit searches to a country by using componentRestrictions. However, the documentation apparently only relates
to JavaScript.
I'm doing a regular request from a PHP application, and need to limit a place
search to a country. However, I can't see how I can limit search results to a country using a regular GET request.
I believe you can just add components=country:XX to your request URL :)

How to get Country for Google Places API nearby search

I'm using Google Places API to get nearby places, but I've noticed that while I get the place names and addresses, the results don't include the Country.
Is there a way to get the country included in the results?
This is the call I'm using:
https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location=<lat,lng>&radius=100&sensor=true&key=<api_key>
Thanks!
The Geocoding API is what you want.
Try
http://maps.googleapis.com/maps/api/geocode/json?address=Calgary,%20Alberta&sensor=false
it'll return fully formatted address info in several ways (split by administrative level, formatted as an address,etc.)
also the place Types blog in google api will help..link is here

Sorting places by distance - Android

I am using Google places API for writing my android app. I want to get a list of places close to current location of the device based on prominence(i.e I do not want to pass "keyword" or "type" or "name" parameter in my URL). Is there a way to do this?
According to the following link, you have to pass keyword, name or type parameter in the URL.
https://developers.google.com/places/training/additional-places-features
I searched google and quite many threads on stackoverflow, but could not find much useful help in using rankby=distance parameter. I would appreciate it if you could share your experiences on sorting places by distance using google places API.
Thank you for your help.
NOTE:
The following URL fails:
https://maps.googleapis.com/maps/api/place/search/json?location=37.787930,-122.4074990&rankby=distance&sensor=false&key=
Keyword, Type and Name are all optional parameters. You only need to pass your API Key, Location and sensor. See the following link for details:
https://developers.google.com/places/documentation/search

Resources