Reputation: 21
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.
Upvotes: 2
Views: 5727
Reputation: 4365
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>
Working in jsFiddle as well: Exemple
NOTE: I'm using vanilla JavaScript. The code might work fine in Chrome, I did not teste it in other browsers.
Upvotes: 2