如何在跑步时更改 Google 地图的语言?

发布于 2024-11-29 18:39:06 字数 105 浏览 1 评论 0原文

我不想反转地理编码并获取两种语言(阿拉伯语和英语)的地址,因此我想获取一种语言的地址,然后更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鹊巢 2024-12-06 18:39:06

加载 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 where XX 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.

沙沙粒小 2024-12-06 18:39:06

我创建了一个函数来更改运行时 Google 地图的语言。
由于它返回一个 Promise,因此您可以轻松地等待 API 加载后再执行其他代码。

演示


使用示例

setAPILanguage('fr', ['places'], 'xxxxxx').then(() => {
    //The API is loaded here
});

文档

/**
 * Changes the language of the Google Maps API
 * @param {String}   lang      - Google Maps new language
 * @param {String[]} libraries - The list of libraries needed
 * @param {String}   key       - Your API key
 * @returns {Promise}          - Resolves when Google Maps API has finished loading
 */

来源

const setAPILanguage = (lang, libraries, key) => {
  //Destroy old API
  document.querySelectorAll('script[src^="https://maps.googleapis.com"]').forEach(script => {
    script.remove();
  });
  if(google) delete google.maps;

  //Generate new Google Maps API script
  let newAPI = document.createElement('script');
  newAPI.src = 'https://maps.googleapis.com/maps/api/js?libraries=' + libraries.join(',') + '&key=' + key + '&language=' + lang + '&callback=googleMapsAPILoaded';

  //Callback for the Google Maps API src
  window.googleMapsAPILoaded = () => {
    let event = new CustomEvent('googleMapsAPILoaded');
    window.dispatchEvent(event);
  }

  //Wait for the callback to be executed
  let apiLoaded = new Promise(resolve => {
    window.addEventListener('googleMapsAPILoaded', () => {
      resolve();
    });
  });

  //Start the script
  document.querySelector('head').appendChild(newAPI);

  return apiLoaded;
}

演示

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

setAPILanguage('fr', ['places'], 'xxxxxx').then(() => {
    //The API is loaded here
});

Documentation

/**
 * Changes the language of the Google Maps API
 * @param {String}   lang      - Google Maps new language
 * @param {String[]} libraries - The list of libraries needed
 * @param {String}   key       - Your API key
 * @returns {Promise}          - Resolves when Google Maps API has finished loading
 */

Source

const setAPILanguage = (lang, libraries, key) => {
  //Destroy old API
  document.querySelectorAll('script[src^="https://maps.googleapis.com"]').forEach(script => {
    script.remove();
  });
  if(google) delete google.maps;

  //Generate new Google Maps API script
  let newAPI = document.createElement('script');
  newAPI.src = 'https://maps.googleapis.com/maps/api/js?libraries=' + libraries.join(',') + '&key=' + key + '&language=' + lang + '&callback=googleMapsAPILoaded';

  //Callback for the Google Maps API src
  window.googleMapsAPILoaded = () => {
    let event = new CustomEvent('googleMapsAPILoaded');
    window.dispatchEvent(event);
  }

  //Wait for the callback to be executed
  let apiLoaded = new Promise(resolve => {
    window.addEventListener('googleMapsAPILoaded', () => {
      resolve();
    });
  });

  //Start the script
  document.querySelector('head').appendChild(newAPI);

  return apiLoaded;
}

Demo

走野 2024-12-06 18:39:06

正如已经指出的那样,一旦加载地图就无法更改,但是,对于那些有能力刷新页面的人来说,这可能有效:

HTML

<script type="text/javascript">
    //load map based on current lang
    var scriptTag = document.createElement('script');

    var currentLang = window.localStorage && window.localStorage.getItem('language');
    var scriptSrc = '//maps.googleapis.com/maps/api/js?libraries=drawing,places&key=YOUR_KEY_HERE';
    if (currentLang)
        scriptSrc = '//maps.googleapis.com/maps/api/js?language=' + currentLang + '&libraries=drawing,places&key=YOUR_KEY_HERE';

    scriptTag.setAttribute('src', scriptSrc);
    scriptTag.setAttribute('async', '');
    document.head.appendChild(scriptTag);
</script>

JS

function changeLangAndReload(lang) {
    window.localStorage.setItem('language', lang);
    window.location.reload();//or use your preferred way to refresh 
}

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

<script type="text/javascript">
    //load map based on current lang
    var scriptTag = document.createElement('script');

    var currentLang = window.localStorage && window.localStorage.getItem('language');
    var scriptSrc = '//maps.googleapis.com/maps/api/js?libraries=drawing,places&key=YOUR_KEY_HERE';
    if (currentLang)
        scriptSrc = '//maps.googleapis.com/maps/api/js?language=' + currentLang + '&libraries=drawing,places&key=YOUR_KEY_HERE';

    scriptTag.setAttribute('src', scriptSrc);
    scriptTag.setAttribute('async', '');
    document.head.appendChild(scriptTag);
</script>

JS

function changeLangAndReload(lang) {
    window.localStorage.setItem('language', lang);
    window.location.reload();//or use your preferred way to refresh 
}
御守 2024-12-06 18:39:06

以下示例显示日语地图并将区域设置为日本:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&language=ja®ion=JP">
</script>

来源

The following example displays a map in Japanese and sets the region to Japan:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&language=ja®ion=JP">
</script>

Source

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文