谷歌Place API用葡萄牙语

Dou*_*ini 9 google-geocoding-api google-places-api

我正在使用Google的Place API来自动填充用户输入的城市名称(网页).加载API时将语言(pt-BR)作为参数传递,文本框正在以葡萄牙语正确填充,但是当执行方法getPlace()时,它将以英语返回结果(country和administrative_area_level_1).

我不确定问题是Google API,它们是葡萄牙语的翻译还是我错过的.我试图加载它通过西班牙语(es),英语(en)和意大利语(它)作为语言的参数并按预期工作.

这是我加载的方式:

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

...以及我如何根据Google的示例代码获得结果:

// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
Run Code Online (Sandbox Code Playgroud)

我也使用反向地理编码API传递place_id,问题是相同的(C#代码).

    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);
Run Code Online (Sandbox Code Playgroud)

我检查了文档并支持pt-BR:Google API支持的语言

Roh*_*hit 0

巴西葡萄牙语的语言代码是“pt_BR”而不是“pt-BR”,因此将链接更改为:

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

事实证明,我错了……要么 pt-BR 要么 pt_BR 有效。

getPlace 不会根据语言返回结果,这是谷歌地图中现有的错误: https://code.google.com/p/gmaps-api-issues/issues/detail ?id=7891

一个黑客解决方案是读取自动完成输入,因为它已转换为指定的语言。如果您使用自动完成搜索框,您可以在其输入文本框中查询,它只有一种文本输入类型。因此您可以执行以下操作来获取指定语言的完整地址:

$("input:text").val()
Run Code Online (Sandbox Code Playgroud)

这将为您提供完整的地址,之后您必须解析该字符串以获取城市、州、国家信息。