如何在跑步时更改 Google 地图的语言?
我不想反转地理编码并获取两种语言(阿拉伯语和英语)的地址,因此我想获取一种语言的地址,然后更改 API 的语言并获取另一种语言的地址,因为我找不到参数发送到地理编码器以确定语言。 有什么建议吗?
I wan't to reverse geocode and get the address in two languages arabic and english so I want to get it in one language then change the language of the API and get the address in the other language, as I can't find a parameter to send to geocoder to determine the language.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
加载 API 时,可以通过将
language=XX
附加到 API URL 来选择语言,其中XX
是两个字符的语言代码 (en 对于英语,
ar
对于阿拉伯语等)添加到 API 调用中的 URL。请参阅 http://code.google.com/apis/maps/documentation/javascript/basics .html#Localization 用于文档。这不会让你即时改变它,我不认为你能做到这一点。在获得所需的一种语言的初始信息后,您可以尝试再次加载 API。但这似乎可能会引起问题。
一种更简洁的方法可能是创建一个单独的页面,为您充当一种 Web 服务。它接受两个参数:语言代码和地址。它使用请求的语言代码加载 API,并对地址进行反向地理编码,提供结果。您的页面将调用这个类似 Web 服务的东西两次,每种语言一次,然后根据需要使用结果。
A language can be selected when you load the API by appending
language=XX
to the API URL whereXX
is the two-character language code (en
for English,ar
for Arabic, etc.) to the URL in the API call. See http://code.google.com/apis/maps/documentation/javascript/basics.html#Localization for documentation.This won't let you change it on the fly, and I don't think you can do that. You can try loading the API a second time after getting the initial info you need in one language. But that seems likely to cause problems.
A cleaner way to do it might be to create a separate page that acts as a sort of web service for you. It accepts two parameters: A language code and an address. It loads the API using the language code requested, and reverse geocodes the address, providing the result. Your page would call this web service-like thing twice, once for each language, and then use the results as desired.
我创建了一个函数来更改运行时 Google 地图的语言。
由于它返回一个
Promise
,因此您可以轻松地等待 API 加载后再执行其他代码。演示
使用示例
文档
来源
演示
I created a function to change the language of Google Maps on the run.
Since it returns a
Promise
, you can easily wait until the API has loaded to execute some other code.Demo
Example of use
Documentation
Source
Demo
正如已经指出的那样,一旦加载地图就无法更改,但是,对于那些有能力刷新页面的人来说,这可能有效:
HTML
JS
As already pointed out this can't be changed once the map is loaded, but, for those who can afford a page refresh, this could work:
HTML
JS
以下示例显示日语地图并将区域设置为日本:
来源
The following example displays a map in Japanese and sets the region to Japan:
Source