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

LcSalazar
  • 16,524
  • 3
  • 37
  • 69
Douglas Gandini
  • 827
  • 10
  • 24
  • Duplicate of http://stackoverflow.com/questions/27567757/inconsistent-language-in-google-place-details-api – aergistal Oct 20 '15 at 13:03
  • 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. – Douglas Gandini Oct 20 '15 at 14:19

1 Answers1

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.

Rohit
  • 300
  • 1
  • 11
  • 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: https://developers.google.com/maps/faq#languagesupport – Douglas Gandini Oct 27 '15 at 11:55
  • 1
    hmm.. i see .. unfortunately.. this is a bug in google maps.. : https://code.google.com/p/gmaps-api-issues/issues/detail?id=7891 – Rohit Oct 27 '15 at 18:15
  • 1
    hey, check out the hacky solution, it could work for you – Rohit Oct 27 '15 at 19:06
  • 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. – Douglas Gandini Oct 27 '15 at 21:13
  • There are good reasons behind the choice of language in all the above cases, see more details (and examples) at http://stackoverflow.com/questions/33520686/how-can-i-get-chinese-words-using-google-api-with-json/37867402#37867402 ─ Places Autocomplete API is an exception to this: the terms returned will match what the user types in. – miguev Jun 16 '16 at 21:01