-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Julian Bissekkou <julian.bissekkou@tapped.dev>
- Loading branch information
1 parent
ec9808e
commit c6055fd
Showing
16 changed files
with
214 additions
and
38 deletions.
There are no files selected for viewing
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
31 changes: 31 additions & 0 deletions
31
android/src/main/java/com/mapbox/mapboxgl/setMapLanguage.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,31 @@ | ||
@file:JvmName("MapboxMapUtils") | ||
|
||
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 | ||
|
||
fun MapboxMap.setMapLanguage(language: String) { | ||
val layers = this.style?.layers ?: emptyList() | ||
|
||
val languageRegex = Regex("(name:[a-z]+)") | ||
|
||
val symbolLayers = layers.filterIsInstance<SymbolLayer>() | ||
|
||
for (layer in symbolLayers) { | ||
// continue when there is no current expression | ||
val expression = layer.textField.expression ?: continue | ||
|
||
// We could skip the current iteration, whenever there is not current language. | ||
if (!expression.toString().contains(languageRegex)) { | ||
continue | ||
} | ||
|
||
val properties = | ||
"[\"coalesce\", [\"get\",\"name:$language\"],[\"get\",\"name:latin\"],[\"get\",\"name\"]]" | ||
|
||
layer.setProperties(PropertyFactory.textField(Expression.raw(properties))) | ||
} | ||
} |
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,78 @@ | ||
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( | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import Mapbox | ||
import MapLibreAnnotationExtension | ||
|
||
/* | ||
* The mapping is based on the values defined here: | ||
|
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,55 @@ | ||
// | ||
// MLNMapView+setLanguage.swift | ||
// maplibre_gl | ||
// | ||
// Created by Julian Bissekkou on 09.08.23. | ||
// | ||
|
||
import Foundation | ||
import Mapbox | ||
|
||
extension MGLMapView { | ||
func setMapLanguage(_ language: String) { | ||
guard let style = style else { return } | ||
|
||
let layers = style.layers | ||
|
||
for layer in layers { | ||
if let symbolLayer = layer as? MGLSymbolStyleLayer { | ||
if symbolLayer.text == nil { | ||
continue | ||
} | ||
|
||
// We could skip the current iteration, whenever there is not current language. | ||
if !symbolLayer.text.description.containsLanguage() { | ||
continue | ||
} | ||
|
||
let properties = ["text-field": "[\"coalesce\",[\"get\",\"name:\(language)\"],[\"get\",\"name:latin\"],[\"get\",\"name\"]]"] | ||
|
||
LayerPropertyConverter.addSymbolProperties( | ||
symbolLayer: symbolLayer, | ||
properties: properties | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
private extension String { | ||
func containsLanguage() -> Bool { | ||
do { | ||
let regex = try NSRegularExpression(pattern: "(name:[a-z]+)") | ||
let range = NSRange(location: 0, length: self.utf16.count) | ||
|
||
if let _ = regex.firstMatch(in: self, options: [], range: range) { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} catch { | ||
return false | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -9,5 +9,5 @@ dependencies: | |
meta: ^1.0.5 | ||
|
||
environment: | ||
sdk: '>=2.12.0 <3.0.0' | ||
sdk: '>=2.14.0 <3.0.0' | ||
flutter: '>=2.10.0' |
Oops, something went wrong.