Skip to content

Commit

Permalink
fix web impl and take case about non lists
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanschaller committed Aug 9, 2023
1 parent 9c27874 commit b678643
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions maplibre_gl_web/lib/src/mapbox_web_gl_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -238,27 +238,30 @@ class MaplibreMapController extends MapLibreGlPlatform
Future<void> setMapLanguage(String language) async {
final List<dynamic> layers = _map.getStyle()?.layers ?? [];

final nameRegex = RegExp("(name:[a-z]+)");

final symbolLayers = layers.where((layer) => layer.type == "symbol");

for (final layer in symbolLayers) {
final List<dynamic>? properties =
_map.getLayoutProperty(layer.id, 'text-field');
final dynamic properties = _map.getLayoutProperty(layer.id, 'text-field');

if (properties == null || properties.isEmpty) {
if (properties == null) {
continue;
}

final List<dynamic> newProperty = properties.map((property) {
if (property is List) {
return property
.map((x) => x.toString().replaceAll(nameRegex, "name:$language"))
.toList();
} else {
return property.toString().replaceAll(nameRegex, "name:$language");
}
}).toList();
final dynamic newProperty;

if (properties is List) {
newProperty = properties.map((property) {
if (property is List) {
return property
.map((x) => x.toString().replaceLanguageWith(language))
.toList();
} else {
return property.toString().replaceLanguageWith(language);
}
}).toList();
} else {
newProperty = properties.toString().replaceLanguageWith(language);
}

_map.setLayoutProperty(layer.id, 'text-field', newProperty);
}
Expand Down Expand Up @@ -1074,3 +1077,11 @@ class MaplibreMapController extends MapLibreGlPlatform
throw UnimplementedError();
}
}

extension on String {
String replaceLanguageWith(String language) {
final nameRegex = RegExp("(name:[a-z]+)");

return this.replaceAll(nameRegex, "name:$language");
}
}

0 comments on commit b678643

Please sign in to comment.