autocomplete to return results within a radius - google-maps

In my google maps autocomplete server side query, i'm trying to return results around or near a latitude and longitude, I've tried this so far
https://maps.googleapis.com/maps/api/place/autocomplete/json?key=<<KEY>>&types=geocode&sensor=true&language=en&location=40.64131109999999%2C-73.77813909999998&strictBounds=true&radius=10&input=london
WHERE 40.64131109999999%2C-73.77813909999998 is the latitude and longitude of JFK - John F. Kennedy International Airport.
But when the search string is london, results still show. Any idea how to limit results within a certain radius / boundary or country? Thanks

Here are my findings on your URL request:
You have included strictBounds=true which is incorrect since parameters are case sensitive hence it will be omitted in the request. It should be strictbounds instead.
You also do not need the =true because adding the parameter strictbounds is enough to restrict the places returned within the defined location and radius.
The value of radius that you have included is too small (10) which may lead to ZERO_RESULTS
You may try this request instead:
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=london&location=40.64131109999999,-73.77813909999998&radius=10000&components=country:US&strictbounds&key=YOUR_API_KEY

Related

Google maps returning two different routes

I am surprised when I am finding the directions between two locations on google maps(maps.google.co.in) one using LAT, LNG, and the other one using the google suggested name. The routes returned are different.
Origin Address - 161 Keltner Ave, Spartanburg, SC 29302, USA.
Destination latitude and longitude - 35.554627, -82.853629
Link for address to address - https://www.google.co.in/maps/dir/161+Keltner+Ave,+Spartanburg,+SC+29302,+USA/769+Champion+Drive,+Canton,+NC,+USA/#35.2324107,-82.6430623,10z/data=!3m1!4b1!4m14!4m13!1m5!1m1!1s0x88579ff345067667:0x5c96ddb575753f29!2m2!1d-81.8723738!2d34.9216748!1m5!1m1!1s0x88598319df45a533:0xe6a9cc5fe0824b95!2m2!1d-82.853377!2d35.5548676!3e0
Link for address to lat-lng - https://www.google.co.in/maps/dir/161+Keltner+Ave,+Spartanburg,+SC+29302,+USA/35.554627,+-82.853629/#35.2321726,-82.404746,9z/data=!3m1!4b1!4m12!4m11!1m5!1m1!1s0x88579ff345067667:0x5c96ddb575753f29!2m2!1d-81.8723738!2d34.9216748!1m3!2m2!1d-82.853629!2d35.554627!3e0
Why is there so much difference? Am I missing something?
I can't make out much of a difference in the two routes. This is the address to address route as I get it from your link:
This is the route I get from your address to lat/long link:
The only significant difference is the end because the lat/long isn't 100% at the address and it appears you need to take a bit of a detour in the latter case:
The measured difference is about 223 m:

Google Place Search does not return result

I need to fetch location data based on given text.
As example if I search Aldi in google map it shows me lot of data with pagination. I need to get that result using google places api.
I tried it with two API calls. But it returns me following result
https://maps.googleapis.com/maps/api/place/textsearch/json?query=Aldi&key=MY_KEY
Result
{
"html_attributions" : [],
"results" : [],
"status" : "ZERO_RESULTS"
}
https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=ALDI&inputtype=textquery&fields=place_id,name,formatted_address,geometry&key=MY_KEY
Result
{
"candidates" : [],
"status" : "ZERO_RESULTS"
}
I need to fetch data based on the given name. Can anyone find out the reason.
There are three types of searches provided by the Places API: Find Place, Nearby Search and Text Search. Each allows you to specify a location with radius to start the search from. The location is specified as a latitude/longitude pair. You received ZERO_RESULTS because you didn't specify a location for your request. If the location parameter is not specified "the API uses IP address biasing by default" according to the documentation. So, there are no Aldi stores within range of the location of your IP address.
Find Place will only return one result though, in my experience, it sometimes returns two. Both Nearby Search and Text Search will return up to 60 place results. All three of the Place search requests allow specifying a radius around your location of up to 50 kilometers. If you need to find Aldi places worldwide you'll need to make quite a few requests.
I am weeks into a similar project to find all locations for a list of restaurant chains in the US. I have found that Nearby Search is a better choice for my use case and should be considered always before committing to Text Search for a project. I've tested Aldi searches with both Nearby Search and Text Search and found that they provide the identical set of place_id results. This Nearby Search request will find all Aldi locations within 50 kilometers of New York City:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.785276%2C-73.9651827&name=Aldi&radius=50000&key=MY_API_KEY
Here is the same as a Text Search:
https://maps.googleapis.com/maps/api/place/textsearch/json?query=Aldi&location=40.785276%2C-73.9651827&radius=50000&key=MY_API_KEY
So why should we care? Text Search according to API documentation "... returns all of the available data fields for the selected place, and you will be billed accordingly." Furthermore "... the Text Search service is subject to a 10-times multiplier. That is, each Text Search request that you make will count as 10 requests against your quota." A Nearby Search request is less expensive and not subject to the 10x multiplier. It returns a subset of the available data fields that you might find sufficient. If you need additional data fields, you can get only what you need from a Places Detail request. Do the math for your application before you select Text Search. It might be dramatically less expensive to implement using Nearby Search followed by Places Detail requests if necessary. In any case, you don't want to be shocked when you hit quota limits unexpectedly because of the 10x multiplier OR the billed transaction costs are more than you expect!
I have found additional hurdles that should be considerations for projects attempting to find all locations for a business in a large area:
The Places API will prefer places within your radius but will include places outside your radius if it determines they are relevant and within the 60 place limit. I have had places returned more than 450 kilometers from my requested search position.
Results are going to be returned for places with names that are NOT what you searched for. In my search for the restaurant Benihana in Seattle a Nearby Search request only returns a restaurant with the name Hamansu. Upon investigation, this is because there is not a Benihana in Seattle, however, Hamansu is similar to Benihana in that it serves Japanese dishes grilled tableside. The API documentation states your search term will be "matched against all content that Google has indexed for this place, including but not limited to name, type, and address, as well as customer reviews and other third-party content."
Results are returned 20 at a time. If there are more results, a page_token is provided to make a request to get the next page of up to 20 results. Each request is chargeable. You will be billed for the 3 requests required to get 60 results. I'm not saying this is bad, just be aware of the expense and quota usage you are incurring with this API.
If there are more than 60 results for your radius then you haven't found all the possible locations within it. And, you can't determine with certainty what the effective radius covered was for the 60 results. You need to search with a small enough radius to return < 60 results for each request. A worldwide search is going to require a large quota and $ budget to pursue.
You should be aware that Places API search is not designed to provide results world wide. In your examples you specify only text value 'Aldi'. However, in order to get results you should specify also where you are searching.
For example, if I want to bias results towards Barcelona area in Spain I have to add location and radius in my request
https://maps.googleapis.com/maps/api/place/textsearch/json?query=Aldi&location=41.3850639%2C2.1734035&radius=10000&key=MY_API_KEY
This request will return Aldi supermarkets in Barcelona area as shown in my screenshot
The same thing for Find place, you should specify location bias
https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=Aldi&inputtype=textquery&fields=formatted_address,geometry,name,place_id&locationbias=circle%3A1000%4041.3850639%2C2.1734035&key=MY_API_KEY
Also note that Find place returns only one result.
I hope this addresses your doubt.
#Art answer, which is marked with higher upvotes, is only partially correct. The answer suggests that the Find Place api (e.g. maps/api/place/findplacefromtext) will usually return 1 result, at most 2. I tend to agree with him. Even if your search hits multiple targets, only one would be returned with the Find Place api. Consequently, he recommends to use Nearby Search or Text Search, both of which would yield at most 60 results.
However, these two searches require some form of location parameter, otherwise they will likely return 0 results, defaulting to using your IP address, as he indicates. But he recommends using a location accompanied with a radius parameter. The problem with this is the radius parameter has a maximum limit. So it will not target all types of things you want if you are searching over the stretch of an entire country, such as the United States.
The truth is you do not need to use the location and radius. There is another option called region. And you can use region to search the entire distance of a country.
What #Art suggested:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=25.7392%2C-80.3103&name=Law%Offices%of%Alex&radius=50000&key=KEY
https://maps.googleapis.com/maps/api/place/textsearch/json?query=Law%Offices%of%Alex&location=25.7392%2C-80.3103&radius=50000&key=KEY
A more encompassing alternative:
https://maps.googleapis.com/maps/api/place/textsearch/json?query=Law%Offices%of%Alex&region=us&key=KEY
You need to specify the location of your search.

Google places radar search with a radius greater than 50,000 meters

I'm trying to find all Panera restaurants within a 200 mile radius of a certain point.
Here is the URL I have been using so far :
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=39.52963,-119.81380&radius=321869&type=restaurant&name=panera&key=your-key-here
However, I have found out that radius has a maximum of 30 miles. Can anyone suggest a quick workaround for me?
Thanks to all in advance.
Open Google Maps and save 8-10 or more points' lat,long value to trigger your request. Then use a loop to find all places within these points. If you want more precise results, increase first trigger points for your requests.
I tried, it works.
Assume we are using php
$triggerPoints = array("lat1,long1", "lat2,long2", "lat3,long3",....);
foreeach(triggerPoints as $t){
$requestUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$t&radius=[YOUR_RADIUS_VALUE]&type=restaurant&name=panera&key=your-key-here";
$results = file_get_contents($requestUrl);
//Do what you want with response JSON data
}
Important: When you choose trigger points, those points should intersect according to your radius value. So you don't miss any restaurants
Business license with for higher usage limits.
Contact Google sales
Google place nearby search maximum 50,000 meters (31 miles) .
if you try enter more than 50,000 it not work proper.
There is another way for find all restaurants in a city.
google provide Text Search Requests
https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurants+in+Reno,NV,89501,USA&key={API_KEY}
query = keyword + in + city Name
for get city name using latitude longitude
http://maps.googleapis.com/maps/api/geocode/json?latlng=39.52963,-119.81380&sensor=true
For more information how to get city name using latitude longitude
https://developers.google.com/maps/documentation/geocoding/start?csw=1#ReverseGeocoding
for more information about how to use Text Search Requests
https://developers.google.com/places/web-service/search

Google geocoding viewports

Does the viewports in the Google geocoding represents the boundary boxes of the location ? Iam trying to use google viewports of particular city or location to search for the latitude and longitude values that fall in those bounding values.
I have two tables called tblproperty and tblemailalerts, tblproperty has got latitude and longitude values and tblemailalerts also got viewport + latitude and longitude values . Now I want to get the sql to get the matching records with accuracy
I want to achieve similar to zoopla search
for example when user selects location as london , properties should be shown within the boundaries of london. If they select london within 1/4 mile radius i should get the properties within the london + properties with 1/4 radius
The viewport bounds parameter only biases the results. It's not a hard limit. You can read about viewport biasing in the API documentation.
However, the viewport returned by Google for a query for "London" does represent a bounding box you can use in your SQL query.
You could also use the OpenStreetMap API. The query to get a bounding box for London is:
http://nominatim.openstreetmap.org/search?q=London&format=json
In there you will find a number of bounding boxes for places called "London" around the world, but the first one is London, United Kingdom.

Google Nearby places search

I was wondering if we could get nearby location by giving the radius parameter around a fixed point. Say i want to get nearby locations only in 10km diameter of a particular location.
Can i do this using google api? or
i have to use some thing else for this?
from: http://code.google.com/apis/maps/documentation/places/
"Certain parameters are required to initiate a Place Search request. As is standard in URLs, all parameters are separated using the ampersand (&) character. The list of parameters and their possible values are enumerated below.
-location (required) — The latitude/longitude around which to retrieve Place information. This must be provided as a google.maps.LatLng object.
-radius (required) — The distance (in meters) within which to return Place results. The recommended best practice is to set radius based on the accuracy of the location signal as given by the location sensor. Note that setting a radius biases results to the indicated area, but may not fully restrict results to the specified area.
You have 2 aptions to search
1) Nearby search returns complete information of each place but it returns up to 20 results on each query and if more places available, it returns a "next page" token.
url="https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="+lat+","+long+"&radius=" +radius+"&types=" + types + "&key=<PUT_YOUR_API_KEY_HERE>";
lat and long are your center coordinates.
radius is measured in meters and is a value up to 50000.
types is the type of Place you are searching according to this listing: https://developers.google.com/places/supported_types . Example: "atm"
2) Radar search that returns a reduced set of information of each place but it returns up to 200 on each query
url="https://maps.googleapis.com/maps/api/place/radarsearch/json?location="+lat+","+long+"&radius=" +radius+"&types=" + types + "&key=<PUT_YOUR_API_KEY_HERE>";
lat and long are your center coordinates.
radius is measured in meters and is a value up to 50000.
types is the type of Place you are searching according to this listing: https://developers.google.com/places/supported_types . Example: "atm"
You have more options to search, keyword & name appart from type.
You can have your results on xml or json format.
Full definition of nearby and radar search is here: https://developers.google.com/places/web-service/search