Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Help wanted] Changing the language of the map on a custom style #250

Closed
JulianBissekkou opened this issue May 31, 2023 · 3 comments
Closed
Labels
help wanted Extra attention is needed

Comments

@JulianBissekkou
Copy link
Collaborator

JulianBissekkou commented May 31, 2023

We are currently having problems changing the language of the map. We quickly debugged the android code and saw that the language is only applied if the layer is a mapbox style layer.
This is not the case and I am sure that this might not be the case for many other developers using maplibre.

Changing the language of a style is still something new to me, but I have an idea how this could work out:
Correct me If i am wrong, but we need access to the layers of a style and we would expose mapView.style.layers on iOS and mapView.getStyle().getLayers() on Android. (Web has a similar api as well)

On dart side we can then update specific layers to apply the new language.

Some example code that can be called from the dart sdk:

final styleLayers = _controller.getStyleLayers(); // calls "mapView.style.layers" on native
for (final layer in styleLayers) {
  if (layer.style != SymbolLayer) continue;
  await _controller.updateLayoutProperty(layer.id, 'text-field', ['get','name:' + language]);
}

Exposing the style layers might be useful for other customizations :)

@JulianBissekkou JulianBissekkou added the help wanted Extra attention is needed label Jun 29, 2023
@m0nac0
Copy link
Collaborator

m0nac0 commented Jun 30, 2023

Exposing the style layers in Flutter is a good idea in principle.

I have thought about this before, also on exposing the layers properties (read-only). That may not be directly relevant for your use case, but I'm documenting it here just in case. My initial approach was to just get the style JSON from the native libraries and implement one method on the Flutter side to parse this into dart objects. However, MapLibre on iOS does not support getting the full style JSON, so you have to write native iOS code for iterating through the style objects, somehow pass them to the Flutter side (probably by converting them to JSON first on the native side), and then on the Dart/Flutter side parse that JSON into Dart objects. Since there are quite a lot of properties for style layers, we might need to auto-generate this code somehow.

For changing a specific property of a style layer, see #138 where I implemented this for layer visibility. I guess the code for setting the language would be similar.
Ideally, we could generalize this code for setting arbitrary properties, as you propose. From the top of my head, I'm not sure though if Android and (especially) iOS MapLibre libraries expose a method to set style layer properties by string. I think you need native code to call native methods/set native properties depending on the property you want to set. E.g. AFAIK you cannot do something like layer.setProperty("visible", "none"), but only something like layer.isVisible = visible. (For your specific use case: the iOS property name for text-field is text docs

So we might again need (auto-generated) native code with a huge switch statement setting the right property on the native objects, depending on the concrete property name. So in pseudo-code something like this:

function setLayoutProperty(String layerId, String propertyName, String propertyValue){
var layer = getLayerById(layerId) //...
switch(propertyName){
  case "text-field":
    layer.text = propertyValue;
  case "visible":
    ...
}
}

(Such code already exists in the form of the LayerPropertyConverter files, which are generated by a script)

But I'd also be happy if someone finds an easier solution.

Does that make sense?

@m0nac0
Copy link
Collaborator

m0nac0 commented Jul 6, 2023

@JulianBissekkou I just noticed that @martinale14 seems to be working on such a generic mechanism for setting layout properties, using the LayerPropertyConverter generated by a script, in flutter-mapbox-gl/maps#1333

@m0nac0
Copy link
Collaborator

m0nac0 commented Dec 1, 2023

Closing since #303 shipped a generic method to set layout properties and #275 fixed the setMapLanguage method.

@m0nac0 m0nac0 closed this as completed Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants