0

I am using google_maps_flutter and want to use custom marker icons (based on a category, I want to use different icons).

CODE

AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

pubspec.yaml google_maps_flutter: ^1.0.6

Maps_view.dart

HashMap bitmaps = new HashMap<String, BitmapDescriptor>();

@override
  void initState() {
    super.initState();
    _createBitMaps();
  }

  void _createBitMaps() {
    List<String> cats = categories; // List of categories
    // Based on the category, I want to choose a different asset. The name of the asset and the name of the category are for this reason the same. 
    for (String cat in cats) {
      BitmapDescriptor.fromAssetImage(
        ImageConfiguration(size: Size(48, 48)), 'lib/assets/categorie_icons_location/' + cat + '.png').then((onValue) {
          setState(() => bitmaps[cat] = onValue);
        }
      );
    }
  }
...
_markers.add(
                       Marker(
                         markerId: MarkerId('_' + docs[i]['title'] + '_' + docs[i]['latitude'] + '_' + docs[i]['longitude']),
                         position: LatLng(double.parse(docs[i]['latitude']), double.parse(docs[i]['longitude'])),
                         // Use custom icon based on 'category' (from a firebase document) 
                         icon: bitmaps[docs[i]['category']],
                         infoWindow: InfoWindow(
                           title: docs[i]['title'],
                           snippet: docs[i]['street'],
                           onTap: () {}
                         )
                       )
                     );

ERROR

The following error appears when I start my app: Cannot enable MyLocation layer as location permissions are not granted

NOTE

As soon as I comment the code with the custom icons, my app runs perfect.

Chris
  • 41
  • 5

2 Answers2

0

You might have used the wrong Android Manifest.xml. Please check out this answer:

https://stackoverflow.com/a/67121766/11708327

Lars
  • 1,250
  • 9
  • 25
0

I had the same error message in my logs. After some muddling, I got a different error:

com.google.maps.api.android.lib6.common.apiexception.b: Failed to decode image. The provided image must be a Bitmap

I added an assets stanza to pubspec.yaml:

assets:
    - assets/

And now both errors are fixed. See Adding assets and images

"Muddling" mostly involved the ordering and invocation of the async functions to acquire my marker locations and fromAssetImage(), including await/.then and changing async functions between initState() and Widget Build(). I suspect the execution order affects the actual exceptions invoked.