Google Maps geocoding API component restriction - google-maps

I'm using the Google Maps Geocoding API web service, and I'm using the components parameter to restrict results to 'London'. My URL query is here:
https://maps.googleapis.com/maps/api/geocode/json?address=<address>&components=administrative_area:london, united kingdom
This works super well and I only get results which are within London. However, if I ask for something definitely not in London, (e.g 'Manchester'), I simply get a default return relating to 'Greater London'. I would rather it simply returns the ZERO_RESULTS status. Is there a way to specify this?
P.S I have tried to use the region parameter, but this isn't as tight as I want it to be.

Using information found on https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering
You can filter by a city (locality) and country as two separate filter components and these can be separated by the pipe (|) symbol. The country is the ISO 3166-1 country code [https://en.wikipedia.org/wiki/ISO_3166-1] therefore GB in order to cover the city of London.
Therefore, this format could be used :
https://maps.googleapis.com/maps/api/geocode/json?address=<address>&components=locality:London|country:UK
With that in mind, and addressing your exact question, according to
https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering
Filter values support the same methods of spelling correction and
partial matching as other geocoding requests. If a geocoding result is
a partial match for a component filter it will contain a partial_match
field in the response.
So it seems it will always try to find partial match when there are no exact matches and this cannot be disabled unfortunately.
However, if you do a check for "partial_match" : true in the result set, you could filter out such results in your code that deals with the response.
In Summary
This format seems to give results without "partial_match" : true if the address is ‘good’
https://maps.googleapis.com/maps/api/geocode/json?address=<address>&components=locality:London|country:UK
And it will allow you to filter the response for "partial_match" : true in your code to exclude spurious results. For example, using an address 'Manchester' :
https://maps.googleapis.com/maps/api/geocode/json?address=Manchester&components=locality:London|country:UK

Related

Google Maps API query with only ZIP code provides incorrect results

We use the Google Maps API, and when a user inputs a 5 digit ZIP code, the results are correct the vast majority of the time; it treats the input as a ZIP code and returns results based on that location.
The problem that has come to our attention is that queries for certain ZIP codes (42025 is our prime example) does NOT return the expected results (Benton, KY). The API call actually returns "status":"ZERO_RESULTS". A similar search on maps.google.com will sometimes produce a positive result, but only if Benton, KY consumes a large portion of the viewport already. A workaround is to add the term "zip" in the query string.
My investigation has shown that the failed results appear to be using the 5 digit string as if a street address, which find nothing. A query for "42020" ends up showing some address near Washington D.C., for example.
Has anyone faced this issue? Any suggestions for how to address this?
I would suggest using a component filtering in your Geocoder requests in order to obtain postal codes.
Your requests will be rewritten as
https://maps.googleapis.com/maps/api/geocode/json?components=postal_code%3A42025%7Ccountry%3AUS&key=YOUR_API_KEY
https://maps.googleapis.com/maps/api/geocode/json?components=postal_code%3A42020%7Ccountry%3AUS&key=YOUR_API_KEY
Both requests return expected results to me. You can also see it in the Geocoder tool:
https://developers-dot-devsite-v2-prod.appspot.com/maps/documentation/utils/geocoder/#q%3D%26options%3Dtrue%26in_country%3DUS%26in_postal_code%3D42025
https://developers-dot-devsite-v2-prod.appspot.com/maps/documentation/utils/geocoder/#q%3D%26options%3Dtrue%26in_country%3DUS%26in_postal_code%3D42020
I hope this helps!
This worked for me. Only with zip code search.
verifyZipCode_google(zipcode) {
return this.httpClient.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${zipcode}&components=postal_code:${zipcode}&region=us&sensor=true&key=${environment.GOOGLE_API_KEY}`);
}

geocode component filtering with field "locality" not working as expected

I'm using Google Map's Geocode API to get the lat, lng values for this address in Manhattan:
123 Dr Martin Luther King Jr Blvd, New York, NY 10035, USA
My initial query looks like this: https://maps.google.com/maps/api/geocode/json?address=123%20mlk%20blvd
This query does not return my desired address, so in order to filter down results to only those in Manhattan I modified by request to include the Component Filtering parameter locality:Manhattan.
As defined in the docs here: https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering
So my question is - why does this url: https://maps.google.com/maps/api/geocode/json?address=123%20mlk%20blvd&components=locality:Manhattan not return my desired address?
And yet - using the component filter postal_code:10035 does return my desired address: https://maps.google.com/maps/api/geocode/json?address=123%20mlk%20blvd&components=postal_code:10035
Thanks
I think the main detail you should be aware of is that Google changed behaviour of components filters recently. This change was made on January 15, 2018. The documentation contains the following text now:
Filters on route, locality, and administrative_area influence the results but do not guarantee containment in the same way as an address query.
source: https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering
That means that currently only postal code and country continue to be enforced strictly for components filters. The Component Filtering in the Geocoding API is no longer as strict as it was before. This change explains behaviour of your sample requests. Component filter with locality is not strict, so you get different result, while component filter with postal code is strict and you get expected result.
If you need a locality you should include it in the address parameter of your request to make it less ambiguous.
E.g.
https://maps.googleapis.com/maps/api/geocode/json?address=123%20mlk%20blvd%20Manhattan&key=YOUR_API_KEY
You should follow the best practices of Geocoding API that are available at:
https://developers.google.com/maps/faq#geocoder_queryformat
https://developers.google.com/maps/documentation/geocoding/best-practices
I hope my answer explains your doubts!

How to filter Google Places API for City term

I want to filter the Google Places API results for a specific City. The filter has to be made by exclusion, not by sorting. I know I can specify the location and radius but that's not enough, because that only priorities the search results.
The level of filtering I desire is the same available for Country.
My urlCall looks like this:
https://maps.googleapis.com/maps/api/place/autocomplete/json?components=country:pt&types=address&input=INPUT&language=en-US&key=MY_KEY
What I'm doing right know is iterating through the JSON result and evaluating if there's a term with the city name.
Thanks in advance!
Other solution (more performance friendly) is to check if the city name is present in the description field. But still, not as good as the country filtering system.
Why don't you match the administrativeArea if your goal is to filter our the city?
The (cities) type collection instructs the Places service to return results that match locality or administrative_area_level_3.
places API resource

Google Maps API v3 Geocoding Status is OK even if geocoded street doesn't exist

I just discovered a strange behavior in Google Maps API V3, and more specifically with its Geocoding tool.
Here is an exemple : This is how I geocode my addresses :
var request = HttpWebRequest.Create(#"http://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=false");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
where address is a varible with a full address entered by the user.
When the address is a valid one, response.StatusCode is OK, which means, according to the documentation, "that no errors occurred; the address was successfully parsed and at least one geocode was returned."
But when the try to geocode this adress :
15 StreetWhoDoesntExist, 69009 LYON
the StatusCode is OK and the latitude / longitude returned is the center of the city (in this example, LYON, which is a real French city and its corresponding zipcode, 69009).
Is this behaviour normal ? When I entered the same address in Google Maps, it told me it can't find this address (which is the "good" behavior). So, how can I reproduce it with GMaps API in order to it refuses addesses with an unknown street name, even if the associated city and/or zipcode exists ?
Thanks in advance !
API CALL
Result:
"partial_match" : true,
"types" : [ "sublocality", "political" ]
So I'd check those two or at least the types to determine if it found the actual street address.
Yes that is normal. There was a result returned, just not the one you were expecting. If you go a bit further down in the documentation you will see this:
geometry contains the following information:
location contains the geocoded latitude, longitude value. For normal address lookups, this
field is typically the most important.
location_type stores additional data about the specified location. The following values are
currently supported:
"ROOFTOP" indicates that the returned result is a precise geocode for which we have location information accurate down to street address precision.
"RANGE_INTERPOLATED" indicates that the returned result reflects an approximation (usually on a road)
interpolated between two precise points (such as intersections).
Interpolated results are generally returned when rooftop geocodes are
unavailable for a street address.
"GEOMETRIC_CENTER" indicates that the returned result is the geometric center of a result such as a
polyline (for example, a street) or polygon (region).
"APPROXIMATE" indicates that the returned result is approximate.
You will need to check the location_type to see how "accurate" the result returned is. In your case the geocoder was unable to find 15 StreetWhoDoesntExist, so the next best result was the center of the zipcode.
The address_components[] that are also returned with the geocode result has additional information you can use to get a better understanding of what is coming back.
The partial_match parameter will let you know if the geocoder did not return and exact match. This will ususally be true with misspellings or missing address parts.
The reason maps.google.com rejects the address is because they have custom logic built into their application that is catching these types of results. The geocoder just returns the raw result, you need to write business logic to handle those results.

how to check the google map api to know the place is existing one

I have use the code given below, please copy and paste in your URL, which gives latitude and longitude of a specific place.
But when I have used the same call with wrong place it returns latitude and longitude. How to check whether it is right and wrong?
http://maps.google.com/maps/api/geocode/json?address=dubai+United+Arab+Emirates&sensor=false
http://maps.google.com/maps/api/geocode/json?address=anu+India&sensor=false
Thanks
A typical scenario for using geocoding will be when you know that the address exists and you want to get the location coordinates, maybe to mark on a map. When you search an address in google geocoding api, it will find exact matches and also partial matches to correct for cases like spelling mistakes or multiple spellings in use. Due to partial match, when you search for a word, words close to that in terms of the letters used and the number of letters present will also be found. You can explore 2 options to refine the results:
1, If you want to avoid partial matches , you can try component filtering , where you specify filters on components like locality, country , etc and only exact matches will be returned.
2, You can process the json result in your code by retreiving different components and doing comparisons and taking only cases matching your requirement.