Change a Map's Language
Map label language in Maptoolkit Maps JS is controlled by the text-field layout property on symbol layers, which reads from a name field in the vector tile data. To switch language, you iterate over all symbol layers and update text-field to reference the appropriate localized name field, such as name:de or name:fr. Use this to build multilingual map applications where the label language should match the user’s browser locale or a dropdown selection.
const API_KEY = 'YOUR_API_KEY';
const map = new maptoolkit.Map({
container: 'map',
apiKey: API_KEY,
style: `https://styles.maptoolkit.net/maptoolkit/maptoolkit.summer.json?api_key=${API_KEY}`,
center: [10.0, 51.0],
zoom: 4,
attributionControl: { compact: false }
});
map.addControl(new maptoolkit.NavigationControl(), 'top-right');
function setLanguage(field) {
if (!map.isStyleLoaded()) return;
const layers = map.getStyle().layers;
for (const layer of layers) {
if (layer.type !== 'symbol') continue;
if (!layer.layout || !layer.layout['text-field']) continue;
const expr = field === 'name'
? ['get', 'name']
: ['coalesce', ['get', field], ['get', 'name']];
map.setLayoutProperty(layer.id, 'text-field', expr);
}
}<!DOCTYPE html>
<html lang="en">
<head>
<title>Change a Map's Language - Maptoolkit Maps JS</title>
<meta property="og:description" content="Switch map label language by updating the text-field expression on symbol layers." />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://unpkg.com/@maptoolkit/[email protected]/dist/maptoolkit.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@maptoolkit/[email protected]/dist/maptoolkit.css" />
<style>
html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
#map { width: 100%; height: 100%; }
#controls {
position: absolute;
top: 10px;
left: 10px;
display: flex;
gap: 6px;
flex-wrap: wrap;
max-width: 320px;
}
#controls button {
padding: 8px 14px;
background: #3887be;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
font-size: 13px;
}
#controls button:hover { background: #2c6d9e; }
</style>
</head>
<body>
<div id="map"></div>
<div id="controls">
<button onclick="setLanguage('name')">Local</button>
<button onclick="setLanguage('name_de')">DE</button>
<button onclick="setLanguage('name_en')">EN</button>
<button onclick="setLanguage('name_fr')">FR</button>
<button onclick="setLanguage('name_it')">IT</button>
<button onclick="setLanguage('name_es')">ES</button>
<button onclick="setLanguage('name_zh')">中文</button>
</div>
<script>
const API_KEY = 'YOUR_API_KEY';
const map = new maptoolkit.Map({
container: 'map',
apiKey: API_KEY,
style: `https://styles.maptoolkit.net/maptoolkit/maptoolkit.summer.json?api_key=${API_KEY}`,
center: [10.0, 51.0],
zoom: 4,
attributionControl: { compact: false }
});
map.addControl(new maptoolkit.NavigationControl(), 'top-right');
function setLanguage(field) {
if (!map.isStyleLoaded()) return;
const layers = map.getStyle().layers;
for (const layer of layers) {
if (layer.type !== 'symbol') continue;
if (!layer.layout || !layer.layout['text-field']) continue;
const expr = field === 'name'
? ['get', 'name']
: ['coalesce', ['get', field], ['get', 'name']];
map.setLayoutProperty(layer.id, 'text-field', expr);
}
}
</script>
</body>
</html>Use the prompt below with any LLM to get the same result. Make sure the Maptoolkit MCP server is connected first — check out AI Integration & MCP to get started.
Use the Maptoolkit Connector. Create an interactive map with zoom level 4, centered around [10.0, 51.0]. Add buttons to switch map label language between Local, DE, EN, FR, IT, ES, and Chinese.