谷歌地图Dyanamic语言的变化 [英] Google Map Dyanamic language change

查看:132
本文介绍了谷歌地图Dyanamic语言的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的一个项目使用backbone.js,并且正在使用Google map api版本3.根据某些ajax响应,我希望随时更改Google地图语言。有没有办法做到这一点。任何建议,将不胜感激。

I am using backbone.js for one of my project, and I am using Google map api version 3. Depending on some ajax response I want to change Google map language on the fly. Is there any ways of doing it.Any suggestion would be appreciated. Thanks in advance.

推荐答案

考虑以下示例,我使用按钮但可以调用用你想要的参数从你的回调中改变GoogleMapsLanguage (使代码适应 lang 参数):

Consider the following example where I'm using a button but you can call the ChangeGoogleMapsLanguage from your callback with the parameters you want (adapting the code to take lang parameter):

<!DOCTYPE html>
<html>
  <head>
    <title>Asynchronous Loading</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script>
        function initialize() {
            var mapOptions = {
                zoom: 8,
                center: new google.maps.LatLng(-34.397, 150.644)
            };

            var map = new google.maps.Map(document.getElementById('map-canvas'),
                mapOptions);
        }

        function loadScript(lang) {
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&' +
                'callback=initialize';
            if (lang) {
                script.src += '&language=' + lang;
            }

            script.id = "google-maps-script";
            document.body.appendChild(script);
        }

        window.onload = loadScript;

        function ChangeGoogleMapsLanguage() {

            var lang = document.getElementById('language').value,
                oldScript = document.getElementById("google-maps-script");
            oldScript.parentNode.removeChild(oldScript);

            delete google.maps;

            loadScript(lang);
        }

    </script>
  </head>
  <body>
    <input type="text" id="language" value="ja" />
    <button id="localization-button" onclick="ChangeGoogleMapsLanguage()">Change Language</button>
    <div id="map-canvas"></div>
  </body>
</html>

在jsFiddle中工作: Exemple

Working in jsFiddle as well: Exemple

注意:我使用的是vanilla JavaScript。该代码在Chrome中可能正常工作,我没有在其他浏览器中对其进行测试。

NOTE: I'm using vanilla JavaScript. The code might work fine in Chrome, I did not teste it in other browsers.

这篇关于谷歌地图Dyanamic语言的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆