mapbox / mapbox-maps-flutter

Interactive, thoroughly customizable maps for Flutter powered by Mapbox Maps SDK
https://www.mapbox.com/mobile-maps-sdk
Other
282 stars 116 forks source link

Android: setStyleJSON not working after upgrading plugin version 2+ #601

Open felixkrautschuk opened 3 months ago

felixkrautschuk commented 3 months ago

After upgrading the plugin from version 1.1.0 to 2.0.0, I have some issues when developing for Android.

I try to set a custom style json like this in the onMapCreated callback:


_onMapCreated(MapboxMap mapboxMap) async {
    this.mapboxMap = mapboxMap;

    final styleJson = await rootBundle.loadString("assets/map-styles/my_style.json");
    mapboxMap.style.setStyleJSON(styleJson);

    print("done");
}

I see the log message in the console, so the code is executed as expected without any error, but I still see the default Mapbox style on Android. It is working as expected on iOS and it was also working on Android using plugin version 1.1.0.

Futhermore, when adding a layer in the onStyleLoadedListener like this:

_onMapStyleLoaded(StyleLoadedEventData styleLoadedEventData) async {
    final linesData = await rootBundle.loadString('assets/map-data/lines.json');
    await mapboxMap?.style.addSource(GeoJsonSource(id: "source_lines", data: linesData));
    await mapboxMap?.style.addLayer(
        LineLayer(
            id: "layer_lines",
            sourceId: "source_lines"
        )
    );
}

I see the added layer, but I also see an exception on Android:

E/flutter (22160): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(Throwable, java.lang.Throwable: Source source_lines already exists, Cause: null, Stacktrace: java.lang.Throwable: Source source_lines already exists E/flutter (22160): at com.mapbox.maps.mapbox_maps.StyleController.addStyleSource(StyleController.kt:288) E/flutter (22160): at com.mapbox.maps.mapbox_maps.pigeons.StyleManager$Companion.setUp$lambda-51$lambda-50(MapInterfaces.kt:5972) E/flutter (22160): at com.mapbox.maps.mapbox_maps.pigeons.StyleManager$Companion.$r8$lambda$DXAQ35E-7m0eWhGSNgtBkwfDJqQ(Unknown Source:0) E/flutter (22160): at com.mapbox.maps.mapbox_maps.pigeons.StyleManager$Companion$$ExternalSyntheticLambda8.onMessage(Unknown Source:2) E/flutter (22160): at io.flutter.plugin.common.BasicMessageChannel$IncomingMessageHandler.onMessage(BasicMessageChannel.java:261) E/flutter (22160): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292) E/flutter (22160): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319) E/flutter (22160): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) E/flutter (22160): at android.os.Handler.handleCallback(Handler.java:958) E/flutter (22160): at android.os.Handler.dispatchMessage(Handler.java:99) E/flutter (22160): at android.os.Looper.loopOnce(Looper.java:205) E/flutter (22160): at android.os.Looper.loop(Looper.java:294) E/flutter (22160): at android.app.ActivityThread.main(ActivityThread.java:8177) E/flutter (22160): at java.lang.reflect.Method.invoke(Native Method) E/flutter (22160): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) E/flutter (22160): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971) E/flutter (22160): , null) E/flutter (22160): #0 StyleManager.addStyleSource (package:mapbox_maps_flutter/src/pigeons/map_interfaces.dart:5816:7) E/flutter (22160): E/flutter (22160): #1 StyleSource.addSource. (package:mapbox_maps_flutter/src/style/style.dart:317:66) E/flutter (22160): E/flutter (22160): #2 MapPageState._onMapStyleLoaded (package:flutter_mapbox/main.dart:48:5) E/flutter (22160):

It is working as expected on iOS and it was also working on Android with plugin 1.1.0.

Full sample project: flutter_mapbox.zip

felixkrautschuk commented 3 months ago

I just noticed that the same PlatformException also occurs on iOS:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(0, Source source_lines already exists, null, null)

0 StyleManager.addStyleSource (package:mapbox_maps_flutter/src/pigeons/map_interfaces.dart:5816:7)

#1 StyleSource.addSource. (package:mapbox_maps_flutter/src/style/style.dart:317:66) #2 MapPageState._onMapStyleLoaded (package:flutter_mapbox/pages/map-page.dart:54:5)
felixkrautschuk commented 3 months ago

on iOS, the issue does not occur when setting styleUri to an empty string

MapWidget(
          //...
          styleUri: "",
          //...
),

but that does not help on Android

mabilinab commented 2 weeks ago

Hey! Any news on this topic? I get the same exact issue when trying to load polyline on Android (no issue on iOS) @felixkrautschuk were you able to find a workaround?

felixkrautschuk commented 1 week ago

@mabilinab adding a delay works for me on Android

_onMapCreated(MapboxMap mapboxMap) async {
    final styleJson = await rootBundle.loadString("assets/map-styles/my_style.json");

    Future.delayed(const Duration(milliseconds: 50), () {
      mapboxMap.style.setStyleJSON(styleJson);
    });
 }

However, I don't like this as there is no guarantee that 50ms is always enough.

mabilinab commented 4 days ago

I was also able to find a workaround: It seems like when you import a custom map URL, you need to use await -- this solved most of the issues for me:

void _onMapCreated(MapboxMap mapboxMap) async {
    _mapboxMap = mapboxMap;
    //Initialize the map settings
    await _mapboxMap!.loadStyleURI("mapbox://styles/XXX/XXXXXX");
  }

  _onStyleLoadedCallback(StyleLoadedEventData data) async {
//.... rest of your code