Getting city from geocode results Google Maps Api - google-maps

I am using the Google Maps API (https://maps.googleapis.com/maps/api/geocode/xml) to geocode places and addresses. However, the results seem inconsistent. The administrative_area_level_2 and locality OR the locality and sublocality_level_1 seem to overlap. But, depending on the address, the overlap is different. One of the things I need to do is determine the city. The documentation states that the locality is the equivalent of a city depending on the country.
If I geocode "Noord-Holland, Nederland" which is a province I get:
country:Nederland
administrative_area_level_1: Noord-Holland
I can understand. All seems well...
If I geocode "Amsterdam, Noord-Holland, Nederland" I get the following result:
country:Nederland
administrative_area_level_1: Noord-Holland
administrative_area_level_2:Amsterdam
locality:Amsterdam
Here I start not understanding the result. Why is Amsterdam listed as an administrative_area_level_2 as well as a locality? Maybe there is a higher governamental body with the same name? But ok, Amsterdam is the locality, thus the city. Which is correct.
If I geocode "Anton de Komplein 150, 1102 CW Amsterdam, Noord-Holland, Nederland" I get the following results:
country:Nederland
administrative_area_level_1:Noord-Holland
administrative_area_level_2:Amsterdam
locality:Amsterdam-Zuidoost
sublocality_level_1:Amsterdam-Zuidoost
Now I really don't understand.
Here is Amsterdam-Zuidoost mentioned twice
Why is the locality here "Amsterdam-Zuidoost"? It is not the city... I would have assumed that "Amsterdam" would be the locality and "Amsterdam-Zuidoost" the sublocality_level_1.
Can someone explain how i should interpretate the results?

I've had a look at your examples and I believe these are data issues that should be reported to Google.
Let's explain my findings:
The request for 'Amsterdam, Noord-Holland, Nederland'
https://maps.googleapis.com/maps/api/geocode/json?address=Amsterdam%2C%20Noord-Holland%2C%20Nederland&key=MY_API_KEY
returns the following address components for me
"address_components":[
{
"long_name":"Amsterdam",
"short_name":"Amsterdam",
"types":[
"locality","political"
]
},
{
"long_name":"Government of Amsterdam",
"short_name":"Government of Amsterdam",
"types":[
"administrative_area_level_2","political"
]
},
{
"long_name":"North Holland",
"short_name":"NH",
"types":[
"administrative_area_level_1","political"
]
},
{
"long_name":"Netherlands",
"short_name":"NL",
"types":[
"country","political"
]
}
]
Note that administrative_area_level_2 has a name 'Government of Amsterdam'. I understand you might be using the Dutch language to get results
https://maps.googleapis.com/maps/api/geocode/json?address=Amsterdam%2C%20Noord-Holland%2C%20Nederland&language=nl&key=MY_API_KEY
In this case I have:
{
"long_name":"Amsterdam",
"short_name":"Amsterdam",
"types":[
"locality","political"
]
},
{
"long_name":"Amsterdam",
"short_name":"Amsterdam",
"types":[
"administrative_area_level_2","political"
]
}
So at this point it looks like the Dutch version is not translated correctly, we have 'Government of Amsterdam' in English and 'Amsterdam' in Dutch. This is a data issue. By the way you can see the administrative area level 2 here:
https://www.google.com/maps/place/Government+of+Amsterdam,+Amsterdam,+Netherlands/#52.3544136,4.7585038,11z/data=!3m1!4b1!4m5!3m4!1s0x47c63fb5949a7755:0xe4f20c148755745!8m2!3d52.3666969!4d4.8945398
The second example is a bit more complicated.
https://maps.googleapis.com/maps/api/geocode/json?address=Anton%20de%20Komplein%20150%2C%201102%20CW%20Amsterdam%2C%20Noord-Holland%2C%20Nederland&key=MY_API_KEY
First of all you should be aware that for street address level searches you will get address components and formatted address in local language as described in the localization of street addresses article. So doesn't matter what language we specify English or Dutch we will see the Amsterdam as the administrative area level 2 and this is exactly the same data issue that we faced in the first example, it should be translated to 'Regering van Amsterdam' on Google side.
Referring to the locality component. It's really weird that Amsterdam-Zuidoost is defined as locality in Google database. I can see it as shown in the screenshot and it looks like a sublocality:
https://www.google.com/maps/place/Amsterdam-Zuidoost,+Netherlands/#52.2569773,4.995519,12z/data=!4m5!3m4!1s0x47c60c069aab8879:0x249b64218cad1744!8m2!3d52.3079989!4d4.9715451
However if you check this feature in the Geocoder tool, it clearly states that this is a locality feature:
https://google-developers.appspot.com/maps/documentation/utils/geocoder/#place_id%3DChIJeYirmgYMxkcRRBetjCFkmyQ
Note the pace ID is ChIJeYirmgYMxkcRRBetjCFkmyQ. I believe this also might be a data issue on Google side.
I would suggest sending a feedback to Google data team following the Google Maps help center:
https://support.google.com/maps/answer/3094045?hl=en&ref_topic=3093612
I hope my answer clarifies your doubts!

Related

Why Google maps API results are not accurate?

We use premium plan for google maps api lookup and encountering accuracy issues while using it I for the following . But these are just examples, there could be many
52241 (should be Coralville, pulling Iowa City )
43065 (should be Powell, pulling Shawnee Hills)
11040 (should be New Hyde park, pulling Garden City ).
we use the following URL:
https://maps.googleapis.com/maps/api/geocode/json?components=country:US|postal_code:
The postal codes that you mentioned in your question overlap several localities. Google shows in address component one locality, probably the most prominent on their opinion.
However, in the response you can see a list of localities that the postal code overlaps. Just look at the postcode_localities field in the response.
E.g., for postal code 52241
https://maps.googleapis.com/maps/api/geocode/json?components=postal_code%3A52241%7Ccountry%3AUS&key=YOUR_API_KEY
you will see
"place_id":"ChIJbaAjdYZD5IcRBfptU5KhMJk",
"postcode_localities":[
"Coralville","Iowa City","Oakdale"
],
"types":[
"postal_code"
]
For postal code 43065
https://maps.googleapis.com/maps/api/geocode/json?components=postal_code%3A43065%7Ccountry%3AUS&key=YOUR_API_KEY
response will contain
"place_id":"ChIJW2TudRTsOIgR8v0-4d19bNA",
"postcode_localities":[
"Powell","Shawnee Hills"
],
"types":[
"postal_code"
]
And finally for postal code 11040
https://maps.googleapis.com/maps/api/geocode/json?components=postal_code%3A11040%7Ccountry%3AUS&key=YOUR_API_KEY
you will get
"place_id":"ChIJ28UCx5hiwokRf_9UXmJHg5I",
"postcode_localities":[
"GARDEN CITY P","Garden City Park","Hillside Manor","Manhasset Hills","New Hyde Park","North Hills","North New Hyde Park"
],
"types":[
"postal_code"
]
Note that localities you were looking for are present in the postcode_localities arrays.
I hope this helps!

Geocoder.geocode({'placeId': placeId}) doesn't return administrative_area_level_2/1

I have problem with result of Geocoder.geocode({'placeId': placeId}) on specific location
(i.e. Main Street 34, Paris) then I will get address_components: street,sublocality,locality,country,postal_code.
function geocodePlaceId(geocoder, placeId, callback) {
geocoder.geocode({'placeId': placeId}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
if (results[0]) {
console.log(results[0]);
callback(results[0].address_components);
}
});
}
code above for the precise location (which contains street nmbr. street and city) returns address_components that are not important for me.
However I want administrative_area_level_2 and administrative_area_level_1 types. Unfortunately I googled a lot and nothing. I thought that this definition for my request can be helpfull
https://developers.google.com/maps/documentation/javascript/3.exp/reference#GeocoderComponentRestrictions but not.
Example: https://maps.googleapis.com/maps/api/geocode/json?&address=luka%20666%20brusno
I created request with exact address with street, number, city and returned address_components which contains only these keys: route, locality, country and postal_code,
but if i change a request to .. address=brusno result contains administrative_area_level_2 and administrative_area_level_1. How can i got administrative_area_level_1 and administrative_area_level_2 for first option?
Do you have some idea how can I resolve this issue or can I influence returned types of address_components?
Based on the Google Maps Geocoding documentation, administrative_area_level_# indicates a civil entity below the country level. And not all nations exhibit these administrative levels. In most cases, administrative_area_level_# short names will closely match ISO 3166-2 subdivisions and other widely circulated lists; however this is not guaranteed as our geocoding results are based on a variety of signals and location data. So maybe that is the issue.
Also, regarding about the request using only the address=brusno, you will notice here that the administrative_area_level_# here is in the Banská Bystrica District not in the place that you use like Luka, Brusno.
If you query that address you will notice that it is the same result.
using address=Banská Bystrica District
https://maps.googleapis.com/maps/api/geocode/json?&address=Bansk%C3%A1%20Bystrica%20District
So maybe like the documentation state that not all place(Luka, Brusno)exhibit these administrative levels.

Distance Matrix API: Zip Code 70128 returns zero results

Here's a little puzzler. If I search for a distance involving area code 70128, like so:
https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6782%2C-73.9442&destinations=new+orleans+LA+70128
I get a ZERO_RESULTS status. But I do get results if I do the same search with only the New Orleans zip code changed to a nearby one, like so:
https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6782%2C-73.9442&destinations=new+orleans+LA+70129
70128 is a legitimate zip code for New Orleans, so I should get results. Interestingly, when I get no results my origin address is displayed just as I gave it (in lat/lng coordinates), but when I do get results it knows to convert it to an address. That may or may not provide a clue.
I've even gone so far as to merely provide a zip code for the origin and destination with similar results.
I can also swap zip code 70128 to an origin instead of a destination. It just seems like the API does not like this zip code for some reason, and I've no idea why that could be. Does anybody have some insight to this?
Result of first URL:
{
destination_addresses: [
"New Orleans, LA 70128, USA"
],
origin_addresses: [
"40.6782,-73.9442"
],
rows: [
{
elements: [
{
status: "ZERO_RESULTS"
}
]
}
],
status: "OK"
}
Result of second URL:
{
destination_addresses: [
"New Orleans, LA 70129, USA"
],
origin_addresses: [
"57 Atlantic Ave, Brooklyn, NY 11216, USA"
],
rows: [
{
elements: [
{
distance: {
text: "1,294 mi",
value: 2081879
},
duration: {
text: "19 hours 7 mins",
value: 68835
},
status: "OK"
}
]
}
],
status: "OK"
}
Looks to me like the 70128 zip code is geolocated in the middle of Lake Pontchartrain. Driving directions will fail if there aren't any roads near that point.
If I take that point and move it to a road (say the I-10 at 30.061671,-89.936772), I get a result
The pin of that zip code is in the water. That is probably the reason why the api returns no result. If you compare that with 70126, you will see that 70126 works because the pin that marks the location of that zip code is not in the water even though most of the area for that zip code marked by Google Maps is in the water.

google.maps.Geocoder.geocode not always returns Australian postcode

I'm using the google.maps.Geocoder class to resolve Australian addresses entered into a free form input field:
pass the entered address (such as "Parramatta") in the google.maps.GeocoderRequest.address field,
call google.maps.Geocoder.geocode
and then show all returned google.maps.GeocoderResult.formatted_address fields to the user so they can select one.
Problem is that the returned formatted_address fields not always contains the postcode.
Pass "Parramatta" into google.maps.GeocoderRequest.address ->
receive formatted_address "Parramatta NSW, Australia"
Pass "Strathfield" into google.maps.GeocoderRequest.address ->
receive formatted_address "Strathfield NSW 2135, Australia"
Looks to me like this is a fault in Google's database, but I'm not sure.
Is there a way to resolve addresses that always gives you the postcode?
If you reverse geocode the location returned for Parramatta, it does contain a post code:
{
"long_name": "2150",
"short_name": "2150",
"types": [
"postal_code"
]
}
That is correct according to Wikipedia

Google maps Places returns zero result. Why?

This URL returns zero results.
https://maps.googleapis.com/maps/api/place/search/json?location=48.015745,37.80285&radius=10000&types=restaurants&name=harbour&sensor=false&key=PUT_YOUR_KEY
It is Donetsk city in Ukraine. Does it mean that in Donetsk we don't have any places?
You're asking for restaurants in a 10000 radius of Donetsk in Ukraine.
The query returns
{
"html_attributions": [ ],
"results": [ ],
"status": "ZERO_RESULTS"
}
It doesn't mean there are no restaurants in Donetsk, but simply that Google Places has no records of such places. I think I saw an hotel on the map, but no restaurants on Maps, no.