I am using google places API, so members can add their location. Then I save these informations in my database. Now would I like to search for all members from "Copenhagen". But this is a problem because members who have used their full address is saved under the danish name "København". The same issue is for all members who live in a city which have a different native and English name. Evens worse it seems like
Is it possible to force the address_components, here locality specific to be in English?
I am pretty sure I have forced the place API to Enligsh, because countries are right. Here it's "Denmark" not "Danmark".
How I use google places. First I register the script:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBQuOpK6nVv7bmGJbB6WoZcnWxKE2yGIGM&libraries=places&language=en"></script>
Next I add the google places autocomplete feature on my input element:
var autocompleteElm = <HTMLInputElement>document.getElementById('autocomplete');
if (autocompleteElm) {
this.autocomplete = new google.maps.places.Autocomplete(
(autocompleteElm), {
types: ['geocode']
});
}
Least but not last I call, after a member hit the save button, I collect the data with a call to the API:
this.autocomplete.getPlace();
This is an Angular2 component written in TypeScript.
I cloned Fiddle example from google, to illustrate my problem: https://jsfiddle.net/dnrn/71t99yzs/
If you search on: "Dortheavej" it shows the city as "København" and not "Copenhagen".
To make matters worse it seems, that sometimes it the country other times it the city that comes not from the "language=en", but the members local..
I know this comes almost 4 years later, but I found your question while searching for a solution to the same problem, so I'll leave this workaround if anyone else should need it.
Do an additional reverse geocoding by postal code of the city you want.
The problem: I needed to save location cities to db uniformly (i.e. in english) no matter what the user's locale is. I also added language=en to all my queries but it didn't seem to work.
Example:
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=St. Basil's Cathedral&language=en returns:
{
"description": "St. Basil's Cathedral, Red Square, Moscow, Russia",
...
"place_id": "ChIJBZbmYllKtUYR5TrL54oWz6Q",
}
So far so good, the city I'm looking for is "Moscow".
However, another search to fetch place details (i.e. locality) https://maps.googleapis.com/maps/api/place/details/json?place_id=ChIJBZbmYllKtUYR5TrL54oWz6Q&language=en returns (among other things):
{
"long_name": "Moskva",
"short_name": "Moskva",
"types": [
"locality",
"political"
]
}
Obviously "Moskva" is not the city name in english, even though the query param was langauge=en. Apparently, this is intended behavior by the API as stated here.
To me it looks like it only localizes the letter being used for the name, or it defaults to latin. So for example, Moscow's original name in russian is "Москва" and they use cyrillic alphabet. So if I set language=en (which uses latin alphabet) it will "translate" it to "Moskva" (which is "Москва" written in latin alphabet, but not in english language). Similarly if I set language=sr, I will receive "Москва" because serbian is offically written in cyrillic as well, so it writes it as the original does. Third option would be language=ja which would also return "Moskva" instead of in japanese (neither language nor alphabet), because it seems to default to latin.
It seems that "official" city names can be translated to the language you've set if it's explicitly the city that you're searching for (and not as part of an address, neighborhood etc).
So in order to get "Moscow" (or the name of Moscow in any other language, or for any other city), you do the following:
place_id from result (as shown in the example above)place_id (also shown above)address_components among other things: "address_components": [
...
{
"long_name": "Moskva",
"short_name": "Moskva",
"types": [
"locality",
"political"
]
},
...
{
"long_name": "Russia",
"short_name": "RU",
"types": [
"country",
"political"
]
},
{
"long_name": "109012",
"short_name": "109012",
"types": [
"postal_code"
]
}
]
Now take postal_code, locality and country and concat them together in a string like so: 109012, Moskva, Russia (almost like a mailing format).
Finally take this string as input for geocode API to do reverse search by address, along with the desired language (en):
https://maps.googleapis.com/maps/api/geocode/json?address=109012, Moskva, Russia&language=en
Now just take the locality which is in english:
{
"long_name": "Moscow",
"short_name": "Moscow",
"types": [
"locality",
"political"
]
}
And that's it!
Known "pitfalls" so far:
postal_code can be null, so make sure you check for that and just exclude it from the string above (it will still work)sublocality_level_1 instead)That's it, hope it helps someone!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With