-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example to test the localizations
- Loading branch information
1 parent
0e36965
commit 48a41d5
Showing
9 changed files
with
119 additions
and
28 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
android/src/main/java/com/mapbox/mapboxgl/MapLibreLocalization.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.mapbox.mapboxgl | ||
|
||
import com.mapbox.mapboxsdk.maps.MapboxMap | ||
import com.mapbox.mapboxsdk.style.expressions.Expression | ||
import com.mapbox.mapboxsdk.style.layers.PropertyFactory | ||
import com.mapbox.mapboxsdk.style.layers.SymbolLayer | ||
|
||
class MapLibreLocalization(private val mapBoxMap: MapboxMap) { | ||
|
||
fun setMapLanguage(language: String) { | ||
val layers = mapBoxMap.style?.layers ?: emptyList() | ||
|
||
val symbolLayers = layers.filterIsInstance<SymbolLayer>() | ||
|
||
for (layer in symbolLayers) { | ||
val expression = layer.textField.expression ?: continue | ||
|
||
val languageNameRegex = Regex("(name:[a-z][a-z])") | ||
|
||
val newExpression = expression | ||
.toString() | ||
.replace(languageNameRegex, "name:$language") | ||
|
||
layer.setProperties(PropertyFactory.textField(Expression.raw(newExpression))) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:maplibre_gl/mapbox_gl.dart'; | ||
|
||
import 'page.dart'; | ||
|
||
class LocalizedMapPage extends ExamplePage { | ||
const LocalizedMapPage({super.key}) | ||
: super(const Icon(Icons.map), 'Localized screen map'); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const LocalizedMap(); | ||
} | ||
} | ||
|
||
class LocalizedMap extends StatefulWidget { | ||
const LocalizedMap({super.key}); | ||
|
||
@override | ||
State createState() => LocalizedMapState(); | ||
} | ||
|
||
class LocalizedMapState extends State<LocalizedMap> { | ||
final _mapReadyCompleter = Completer<MaplibreMapController>(); | ||
|
||
var _mapLanguage = "en"; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
body: Column( | ||
children: [ | ||
DropdownButton<String>( | ||
value: _mapLanguage, | ||
icon: const Icon(Icons.arrow_drop_down), | ||
elevation: 16, | ||
onChanged: (value) { | ||
if (value == null) return; | ||
|
||
setState(() => _mapLanguage = value); | ||
_setMapLanguage(); | ||
}, | ||
items: ["en", "de", "es", "pl"] | ||
.map<DropdownMenuItem<String>>((String value) { | ||
return DropdownMenuItem<String>( | ||
value: value, | ||
child: Text(value), | ||
); | ||
}).toList(), | ||
), | ||
Expanded( | ||
child: MaplibreMap( | ||
styleString: | ||
"https://api.maptiler.com/maps/3dd4d51b-ae78-4074-8b31-b47a49f1b5ce/style.json?key=kZ5xAKKbPzxo3GeJ2odT", | ||
onMapCreated: _onMapCreated, | ||
initialCameraPosition: | ||
const CameraPosition(target: LatLng(0.0, 0.0)), | ||
onStyleLoadedCallback: _onStyleLoadedCallback, | ||
), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
|
||
void _onMapCreated(MaplibreMapController controller) { | ||
_mapReadyCompleter.complete(controller); | ||
} | ||
|
||
void _onStyleLoadedCallback() { | ||
_setMapLanguage(); | ||
} | ||
|
||
Future<void> _setMapLanguage() async { | ||
final controller = await _mapReadyCompleter.future; | ||
controller.setMapLanguage(_mapLanguage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters