flutterdartgoogle-mapsgoogle-maps-flutter

Flutter Google Maps: setMapStyle deprecated, how to style the map in newer versions?


I'm using the latest version of google_maps_flutter, and I noticed that this code is now showing a deprecation warning:

if (_mapStyle != null) {
  mapController.setMapStyle(_mapStyle);
}

I used this to apply a custom JSON map style after the map was created.
But now setMapStyle is marked as deprecated, and the documentation doesn't clearly show the new recommended way.
I tried looking for an alternative like applyMapStyle or other methods on GoogleMapController, but none seems to replace it.


Solution

  • Use the mapStyle parameter on the GoogleMap widget:

    final String mapStyleJson = '''[ ... your JSON ... ]''';
    
    GoogleMap(
      initialCameraPosition: const CameraPosition(
        target: LatLng(10.0, 76.0),
        zoom: 14,
      ),
      mapStyle: mapStyleJson,
      onMapCreated: (controller) {
        mapController = controller;
      },
    )
    

    You can check the full example in the official Google Maps Flutter package.