Skip to content

Commit

Permalink
Only localize layers based on Mapbox Streets source #3
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmartinelli committed May 17, 2017
1 parent 5bf29b8 commit e732d2c
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function isNameFunctionField(property) {
function adaptPropertyLanguage(property, languageFieldName) {
if (isNameStringField(property)) return languageFieldName;
if (isNameFunctionField(property)) {
const newStops = property.stops.map(function (stop) {
var newStops = property.stops.map(function (stop) {
if (stop[1].startsWith('{name')) {
return [stop[0], languageFieldName];
}
Expand All @@ -57,15 +57,26 @@ function changeLayerTextProperty(layer, languageFieldName) {
return layer;
}

function findStreetsSource(style) {
var sources = Object.keys(style.sources).filter(function (sourceName) {
var source = style.sources[sourceName];
return /mapbox-streets-v\d/.test(source.url);
});
return sources[0];
}

/**
* Change the language field for a style.
* @param {object} style - Mapbox GL style
*/
MapboxBrowserLanguage.prototype.changeLanguage = function (style) {
var field = this.options.getLanguageField();
var streetsSource = findStreetsSource(style);
if (!streetsSource) return style;

var changedLayers = style.layers.map(function (layer) {
return changeLayerTextProperty(layer, field);
if (layer.source === streetsSource) return changeLayerTextProperty(layer, field);
return layer;
});

var languageStyle = Object.assign({}, style, {
Expand Down

0 comments on commit e732d2c

Please sign in to comment.