How to Use Google Directions API in Flutter

Google Directions API is a powerful tool for developers to get directions and travel times between two locations. It can be used to generate routes, calculate distances, and provide estimated travel times for various transportation modes like walking, cycling, or driving. In this tutorial, we’ll look at how to use Google Directions API in Flutter.

flutter directions api

What is Google's Directions API?

Google Directions API is a web service that provides directions between two or more locations. It is part of the Google Maps Platform and provides a comprehensive set of APIs that allow developers to build custom applications with powerful mapping and routing capabilities. The API allows developers to generate directions with detailed information such as turn-by-turn instructions, travel times, and estimated distances.

Prerequisites

In order to use Google Directions API in Flutter, you’ll need the following:

  • A Google Cloud Platform account.
  • A Google Maps Platform API Key.
  • The Flutter SDK installed.
  • An IDE such as Android Studio or Visual Studio Code.

Setting Up The Flutter Project

To get started, create a new Flutter project in your IDE.

Open the pubspec.yaml file and add the google_maps_webservice package to the dependencies section:

dependencies:
  google_maps_webservice: ^0.2.2

Then, run the following command to install the package:

flutter pub get

Installing Dependencies & Setup

Once the package is installed, you’ll need to set up the Google Maps Platform API Key. To do this, open the Google Cloud Platform Console and click on the “APIs & Services” tab. Then, click on “Credentials” and create a new API Key.

Next, open the AndroidManifest.xml file in the android/app/src/main/ folder and add the following line to the application section:

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

Replace YOUR_API_KEY with your own API key.

How to Use Google Directions API in Flutter?

Using the Google Directions API in Flutter is quite simple. You’ll need to call the Google Directions API with the start and end locations, and then parse the response.

Here’s an example of how to make a request to the Directions API and parse the response:

import 'package:google_maps_webservice/directions.dart';

final GoogleMapsDirections _directions =
    GoogleMapsDirections(apiKey: YOUR_API_KEY);

final DirectionsResponse response = await _directions.directionsWithLocation(
  Location(startLatitude, startLongitude),
  Location(endLatitude, endLongitude),
);

if (response.isOkay) {
  // Do something with the response
}

How to Draw a Route on a Map in Flutter?

Once you’ve received a response from the Directions API, you can use the response to draw a route on a map.

To draw a route on a map, you’ll need to use the polyline package. To install the package, add the following line to the pubspec.yaml file:

dependencies:
  polyline: ^1.0.0

Then, run the following command to install the package:

flutter pub get

Once the package is installed, you can use the response from the Directions API to draw a route on the map. Here’s an example of how to do it:

import 'package:google_maps_webservice/directions.dart';
import 'package:polyline/polyline.dart';

final GoogleMapsDirections _directions =
    GoogleMapsDirections(apiKey: YOUR_API_KEY);

final DirectionsResponse response = await _directions.directionsWithLocation(
  Location(startLatitude, startLongitude),
  Location(endLatitude, endLongitude),
);

if (response.isOkay) {
  // Get the encoded polyline from the response
  final String encodedPolyline = response.routes[0].overviewPolyline.points;
  
  // Decode the polyline
  final List<PointLatLng> points = decodePolyline(encodedPolyline);
  
  // Create a Polyline
  final Polyline polyline = Polyline(
    points: points,
    strokeWidth: 5,
    color: Colors.blue,
  );
  
  // Add the Polyline to the map
  mapController.addPolyline(polyline);
}

Various Use Cases for Google's Directions API in Mobile Apps

Google Directions API can be used in a variety of mobile applications. It can be used to generate turn-by-turn navigation instructions for applications like ridesharing and delivery services. It can also be used to calculate the estimated travel time between two locations for applications like ride-hailing. Additionally, it can be used to generate routes for applications like fitness and running apps.