Google Place API in portuguese
Asked Answered
M

1

9

I'm using Google's Place API to Autocomplete the cities names typed by the users (webpage). The API is loaded passing the language (pt-BR) as parameter and the text box is being filled correctly in portuguese, but when the method getPlace() is executed it returns the results (country and administrative_area_level_1) in English.

I'm not sure if the problem is the Google API, their translation to portuguese or something that I missed. I've tried to load It passing spanish (es), english (en) and italian (it) as language's parameter and working as expected.

Here is how I am loading:

<script src="https://maps.googleapis.com/maps/api/js?key=[myapikey]&signed_in=true&libraries=places&callback=initAutocomplete&language=pt-BR" async defer></script>

... and how I am getting the results based on Google's Sample code:

// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();

I'm also using the reverse geocoding API passing the place_id and the issue is the same (C# code).

    var apikey = "[myserverapikey]";
    var requestUri = string.Format("https://maps.googleapis.com/maps/api/geocode/json?place_id={0}&key={1}", place_id, apikey);

    var idiomas = new string[] {"pt-BR", "es", "en-us", "it"};

    string responseContent = string.Empty;
    foreach (var idioma in idiomas)
    {
        HttpWebRequest request = WebRequest.Create(requestUri) as HttpWebRequest;
        request.Headers.Add(HttpRequestHeader.AcceptLanguage, idioma);
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        StreamReader reader = new StreamReader(response.GetResponseStream());

        var jsontext = reader.ReadToEnd().Trim();

        responseContent += jsontext;

        var json = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(jsontext);


    }

    return Json(responseContent, JsonRequestBehavior.AllowGet);

I've checked the documentation and the pt-BR is supported: Google API Supported Languages

Morel answered 16/10, 2015 at 21:37 Comment(2)
Duplicate of #27568257Favourite
The linked answer says that this occours when the translation is unavailable, but it's not my case since the text box is being filled correctly in pt-BR. It's weird but possible depending on Google's data structure.Morel
R
0

The language code for Brazilian Portuguese is "pt_BR" instead of "pt-BR" so change the link to:

<script src="https://maps.googleapis.com/maps/api/js?key=[myapikey]&signed_in=true&libraries=places&callback=initAutocomplete&language=pt_BR" async defer></script> >

Turns out, I was wrong... either pt-BR or pt_BR work.

The getPlace does not return results according to the language, this is an existing bug in google maps: https://code.google.com/p/gmaps-api-issues/issues/detail?id=7891

A hacky solution is to read the autocomplete input since it is converted to the language specified. If you are using the autocomplete search box you can query on its input text box, it only has one input type of text.. so you can do the following to get the full address in the language specified:

$("input:text").val()

This will give you the full address, after that you will have to parse the string to get the city,state,country info.

Richthofen answered 26/10, 2015 at 18:34 Comment(5)
Thanks for your answer, but the documentation says that is pt-BR. Anyway I tried your suggestion and It didn't work also. Supported languages: developers.google.com/maps/faq#languagesupportMorel
hmm.. i see .. unfortunately.. this is a bug in google maps.. : code.google.com/p/gmaps-api-issues/issues/detail?id=7891Richthofen
hey, check out the hacky solution, it could work for youRichthofen
Thanks again! I've already tried this but Google Places API doesn't have a consistent return pattern for the display name. Eg: it returns "São Paulo - SP, Brasil" using a dash to separate the city name (São Paulo) and the administrative region (SP), but if I search for New York it uses a comma ("New York, Nova Iorque, Estados Unidos"). If I search for Nova Iorque it returns "Nova Iorque, Estados Unidos" without the administrative region. Both searchs for NYC returns the same place_id.Morel
There are good reasons behind the choice of language in all the above cases, see more details (and examples) at #33521186 ─ Places Autocomplete API is an exception to this: the terms returned will match what the user types in.Sough

© 2022 - 2024 — McMap. All rights reserved.