Open In App

How to Integrate Google Maps in Flutter Applications?

Last Updated : 22 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

We all have seen that Google Maps is used by many applications such as Ola, Uber, Swiggy, and others. We must set up an API project with the Google Maps Platform in order to integrate Google Maps into our Flutter application. In this article, we will look at How to Integrate Google Maps in Flutter Applications?

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.

Step 2: Generating API Key for Using Google Maps

Check out the below article on How to generate an API key for using Google Maps. After developing that key we have to use it within our project to integrate Google Maps. 

Now, follow the Steps to Implement Google Maps in the Flutter Application

Step 3: Adding Dependency to pubspec.yaml File

dependencies:
    flutter: 
    google_maps_flutter: ^2.1.8

Now click on pub.get to configure.

Step 4: Update build.gradle File.

Navigate to android>app>build.gradle file and update code on below line. 

Change Compile SDK Version

android {
    compileSdkVersion 31

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

Change Min SDK Version

defaultConfig {
    // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
    applicationId "com.example.google_maps"
    minSdkVersion 20
    targetSdkVersion 30
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
}

Step 5: Add API Key to Android Manifest File

Navigate to android > app > src > main > AndrodManifest.xml file and add the below code in the manifest tag. Make sure to add your Google Maps API key in the value. 

<meta-data android:name="com.google.android.geo.API_KEY"
        android:value="Enter your API key here"/>

Step 6: Working with main.dart File. 

Navigate to lib > main.dart file and add the below code to it. Comments are added in the code to get to know in detail. 

Dart




void main() {
  runApp(const MyApp());
}
  
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // in the below line, we are specifying title of our app. 
      title: 'GFG',
      // in the below line, we are hiding our debug banner. 
      debugShowCheckedModeBanner: false,
      // in the below line, we are specifying theme. 
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      // First Screen of our App
      home: const HomePage(),
    );
  }
}


Step 7: Creating a new Dart File for Home Page. 

Navigate to lib > Right click on it > New > Dart File and name it as HomePage.dart. After creating that file add the below code to it. Comments are added in the code to get to know in detail. 

Dart




import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
  
class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);
  @override
  _HomePageState createState() => _HomePageState();
}
  
class _HomePageState extends State<HomePage> {
      
  // in the below line, we are initializing our controller for google maps. 
  Completer<GoogleMapController> _controller = Completer();
  
  // in the below line, we are specifying our camera position
  static final CameraPosition _kGoogle = const CameraPosition(
      target: LatLng(37.42796133580664, -122.885749655962),
    zoom: 14.4746,
  );
    
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // in the below line, we are specifying our app bar.
      appBar: AppBar(
        // setting background color for app bar 
        backgroundColor: Color(0xFF0F9D58),
        // setting title for app bar. 
        title: Text("Google Maps"),
      ),
      body: Container(
        // in the below line, creating google maps. 
        child: GoogleMap(
          // in the below line, setting camera position 
            initialCameraPosition: _kGoogle,
          // in the below line, specifying map type. 
          mapType: MapType.normal,
          // in the below line, setting user location enabled.
          myLocationEnabled: true,
          // in the below line, setting compass enabled. 
          compassEnabled: true,
          // in the below line, specifying controller on map complete. 
          onMapCreated: (GoogleMapController controller){
              _controller.complete(controller);
          },
        ),
      )
    );
  }
}


Output



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads