DEV Community

Cover image for How to Add Custom Markers on Google Maps in Flutter?
Don Arias
Don Arias

Posted on

1

How to Add Custom Markers on Google Maps in Flutter?

Flutter offers great flexibility when it comes to customizing maps, especially with the google_maps_flutter: ^2.5.0 plugin. In this tutorial, we will explore how to replace the standard marker icon with any custom widget using the screenshot: ^2.1.0 plugin.

Step 1: Create a Flutter project

If you haven't installed Flutter yet, follow the instructions on the official Flutter website to do so. After that, create a new Flutter project using the following command:

flutter create mon_projet_maps
cd mon_projet_maps
Enter fullscreen mode Exit fullscreen mode

Step 2: Set Up Google Maps
Add the google_maps_flutter plugin to your pubspec.yaml file and run flutter pub get to install the dependencies.

dependencies:
  google_maps_flutter: ^2.5.0
Enter fullscreen mode Exit fullscreen mode

Now, initialize Google Maps in your application. Make sure to add your Google Maps API key. You can follow the instructions here

Step 3: Create a Replacement Widget
Create the widget you want to use as a replacement for the marker icon. For example, let's take this example:

///Custom widget representing a marker on the map.
class CustomMarker extends StatelessWidget {
const CustomMarker({super.key, required this.name, required this.image});
final String name;
final String image;
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: const EdgeInsets.all(5),
decoration: BoxDecoration(
color: const Color(0xffFEC83A),
borderRadius: BorderRadius.circular(10),
),
child: Text(
name,
style: Theme.of(context)
.textTheme
.bodyLarge!
.copyWith(fontWeight: FontWeight.w700, fontSize: 15),
),
),
const SizedBox(
height: 10,
),
Container(
padding: const EdgeInsets.all(5), // Border width
decoration: const BoxDecoration(
color: Color(0xffFEC83A), shape: BoxShape.circle),
child: ClipOval(
child: SizedBox.fromSize(
size: const Size.fromRadius(18), // Image radius
child: Image.network(image, fit: BoxFit.cover),
// child: Image.asset(image, fit: BoxFit.cover),
),
),
)
],
);
}
}

Step 4: Use the screenshot plugin for BitmapDescriptor
Add the screenshot plugin to your pubspec.yaml file:

dependencies:
  google_maps_flutter: ^2.5.0
  screenshot: ^2.1.0
Enter fullscreen mode Exit fullscreen mode

Use this plugin to capture the custom widget and convert it into a BitmapDescriptor:

///Generates a custom [BitmapDescriptor] from a [CustomMarker] widget.
Future<BitmapDescriptor> customMarkerIcon({
required String image,
required String name,
}) async {
final Uint8List result = await ScreenshotController().captureFromWidget(
CustomMarker(
image: image,
name: name,
),
context: context,
targetSize: const Size(200, 150),
pixelRatio: 3.0
);
return BitmapDescriptor.fromBytes(result);
}

Now, use the customMarkerIcon function to obtain your custom BitmapDescriptor and use it when adding markers to the map.

If you want to find a complete example check out this repository.

Ps: Don't forget to leave a comment if you enjoyed.

Sentry mobile image

Mobile Vitals: A first step to Faster Apps

Slow startup times, UI hangs, and frozen frames frustrate users—but they’re also fixable. Mobile Vitals help you measure and understand these performance issues so you can optimize your app’s speed and responsiveness. Learn how to use them to reduce friction and improve user experience.

Read the guide

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs