Adding GIFs as Google Map Markers in Flutter

Nandhu Raj
3 min readSep 14, 2023

--

If you’re building a Flutter app that incorporates Google Maps, you may have encountered the need to customize your map markers. While Google Maps provides default markers, sometimes you want to add a personal touch to your map by using GIFs as markers. Fortunately, Flutter makes it relatively easy to achieve this creative customization. In this blog post, we’ll walk you through the process of adding GIFs as Google Map markers in Flutter.

Prerequisites

Before we get started, make sure you have the following prerequisites in place:

  1. Flutter Installed: Ensure that you have Flutter installed on your development environment. If not, you can follow the official Flutter installation guide on the Flutter website.
  2. Google Maps API Key: You will need a Google Maps API key for your Flutter project. Follow the instructions provided in the Google Maps documentation to obtain one.

Setting Up Your Flutter Project

Once you have Flutter installed and your Google Maps API key ready, it’s time to set up your Flutter project.

  1. Create a New Flutter Project: Open your terminal and create a new Flutter project using the following command:
flutter create gif_marker_map

2. Add Dependencies: Open your pubspec.yaml file and add the necessary dependencies for Google Maps and GIF rendering:

dependencies:
flutter:
sdk: flutter
google_maps_flutter: ^2.0.11
gif: ^0.0.4

Run flutter pub get to fetch the packages.

3. Enable Google Maps: In your android/app/src/main/AndroidManifest.xml file, add your Google Maps API key within the <application> element:

<application>
<!-- ... -->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY_HERE" />
<!-- ... -->
</application>

4. Initialize Google Maps: In your Flutter app, import the necessary libraries and initialize Google Maps. Create a new Flutter widget for your map:

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class GifMarkerMap extends StatefulWidget {
@override
_GifMarkerMapState createState() => _GifMarkerMapState();
}

class _GifMarkerMapState extends State<GifMarkerMap> {
GoogleMapController _mapController;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('GIF Marker Map'),
),
body: GoogleMap(
onMapCreated: (controller) {
_mapController = controller;
},
initialCameraPosition: CameraPosition(
target: LatLng(37.7749, -122.4194), // Set your initial map position
zoom: 12.0,
),
),
);
}
}

void main() {
runApp(MaterialApp(
home: GifMarkerMap(),
));
}

Adding GIF Markers

Now that you have your basic map set up, it’s time to add GIF markers to it. You’ll need GIF files for your markers, and you can easily find them online or create your own. Place your GIF files in the project directory.

  1. Import GIF Package: Import the gif package at the top of your Dart file:
import 'package:gif/gif.dart';

2. Define Custom Markers: Create a function to add custom GIF markers to your map. You can customize the marker by providing the GIF file path and the marker’s position:

Future<void> _addCustomMarker(LatLng position, String gifFilePath) async {
final gifBytes = await GifDecoder().decodeBytes(await rootBundle.load(gifFilePath));

final customIcon = BitmapDescriptor.fromBytes(Uint8List.fromList(gifBytes[0].list));

_mapController.addMarker(
MarkerOptions(
position: position,
icon: customIcon,
),
);
}

3. Add Custom Markers: Call the _addCustomMarker function within the onMapCreated callback or any other appropriate location in your app to add GIF markers:

@override
void initState() {
super.initState();
// Add GIF markers
_addCustomMarker(LatLng(37.7749, -122.4194), 'assets/marker.gif');
}

In this example, we’ve added a GIF marker at the initial map position. You can call _addCustomMarker with different positions and GIF files as needed for your project.

Conclusion

Customizing Google Map markers in your Flutter app with GIFs can add a unique and engaging element to your maps. By following the steps outlined in this blog post, you’ll be able to create interactive and visually appealing maps that enhance the user experience. Feel free to explore further customization options to make your map markers even more impressive. Happy coding!

--

--

Nandhu Raj

It's like being the lead detective in a thrilling crime movie, but with a twist - I'm also the one behind the crime!