javascript 更改 Geocoder.geocode() 返回结果的语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8328701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:03:54  来源:igfitidea点击:

Change the language in which Geocoder.geocode() returns results

javascriptgoogle-mapsgoogle-maps-api-3

提问by 0xbadf00d

As the APIreference states:

正如API参考所述:

The Geocoding API defines a geocoding request using the following URL parameters:

 - language (optional) — The language in which to return results. See the supported list of domain languages. Note that we often update supported languages so this list may not be exhaustive. If language is not supplied, the geocoder will attempt to use the native language of the domain from which the request is sent wherever possible.

However, specifying the languageparameter doesn't seem to have effect (tested with Firefox 8, IE 9 and Chrome 15).

但是,指定language参数似乎没有效果(使用 Firefox 8、IE 9 和 Chrome 15 测试)。

new google.maps.Geocoder().geocode({
    'latLng'  : new google.maps.LatLng(position.coords.latitude, position.coords.longitude),
    'language': 'en'}, 
    function(results, status) {}
);

回答by omarello

The API link you are refering to is not that same as what you are using in your code example.

您引用的 API 链接与您在代码示例中使用的不同。

I beleive what you are looking for is this API, which makes me wonder how the above returns any restuls, since it is expecting a boundsand not latLng, and also it does not support the languagekey.

我相信你正在寻找的是这个 API,这让我想知道上面如何返回任何restuls,因为它期待 abounds和 not latLng,而且它也不支持language密钥。

However in order to get the results in another language you can change the way you include your google maps script according to this section of the docslike so

但是,为了获得另一种语言的结果,您可以根据文档的这一部分更改包含谷歌地图脚本的方式,如下所示

<script type="text/javascript" 
src="http://maps.googleapis.com/maps/api/js?sensor=false&language=ja"></script>

On the other hand if you are really working with the Geocoding API (Web service), then your url requests need to look something like this (changing the json to xml if you want xml out, and changing en to whatever language you want)

另一方面,如果您真的使用Geocoding API (Web service),那么您的 url 请求需要看起来像这样(如果您想要 xml,请将 json 更改为 xml,并将 en 更改为您想要的任何语言)

http://maps.googleapis.com/maps/api/geocode/json?latlng=YOUR-LAT-LNG&language=en