How to get estimated travel time and distance in flutter

Filip Raj picture Filip Raj · Jul 2, 2018 · Viewed 10.2k times · Source

I'm trying to get estimated time and distance between users current location and predefined destination, and I cannot find a way to do this. I know that flutter now has map plugin - flutter maps but I do not need to display map, just to use map api to calculate estimated travel time and distance. Something like MKDirections. Am I missing something? Is there any way to do this?

Answer

Bostrot picture Bostrot · Jul 2, 2018

You can use Google's Distance Matrix API and send simple http request to:

https://maps.googleapis.com/maps/api/distancematrix/json
?units=imperial
&origins=40.6655101,-73.89188969999998
&destinations=40.6905615%2C,-73.9976592
&key=YOUR_API_KEY

where origins are the starting points and destinations are the ending points. Replace them with your values.

For the request you could use the package dio:

import 'package:dio/dio.dart';
Dio dio = new Dio();
Response response=await dio.get("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=40.6905615%2C,-73.9976592&key=YOUR_API_KEY");
print(response.data);