Home > Blockchain >  How can I use distance matrix API in flutter. Can anyone help me
How can I use distance matrix API in flutter. Can anyone help me

Time:12-24

I am making my final year project and I am stuck in distance matrix. I am making an app in which I am using google maps, but I don't know how to find the time and distance between two places. I read some articles but can't understand I am beginner of flutter.

CodePudding user response:

First you need a Google Maps API key, and to get that you need a Google cloud account, it's free to create one, and you get a free trial as well, which should enough for a study project. This link may be helpful for setting up an account and a project : https://developers.google.com/maps/documentation/distance-matrix/cloud-setup

After account and project setup you need to enable the distance matrix API

Then create an API key

Once you have your API key, you are ready to start using the API: All you need to make an API call is your API key and the origin and destination locations (for that you can use latitude/longitude, full address, place IDs...). Here is an example using latitude, longitude coordinates:

https://maps.googleapis.com/maps/api/distancematrix/json?destinations=40.659569,-73.933783&origins=40.6655101,-73.89188969999998&key=**YOUR_API_KEY_HERE**

You can provide many locations separated by a pipe(|) for both the origin and the destination(e.g:11.45455,-13.4675434|12.353645,-32.463634|11.45455,-13.4675434...), the API will match each single origin location to all the destinations and return distance for each pair (basically one request will return origins times destinations results).

NOTE: the example I provided is very simple, but the distance matrix has more feature (e.g: taking into account traffic) visit this link for an overview of the features of the API.

Flutter

To call the API in a flutter app, you will need an HTTP client such as DIO:

import 'package:dio/dio.dart';
void getDistanceMatrix() async {
  try {
    var response = await Dio().get('https://maps.googleapis.com/maps/api/distancematrix/json?destinations=40.659569,-73.933783&origins=40.6655101,-73.89188969999998&key=YOUR_API_KEY_HERE');
    print(response);
  } catch (e) {
    print(e);
  }
}

To see what type of response you will get visit this link.

CodePudding user response:

you can find the distance by the HaverSine formula, implemented in dart as:

import'dart:math' as Math;
void main()=>print(getDistanceFromLatLonInKm(73.4545,73.4545,83.5454,83.5454));



double getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2-lat1);  // deg2rad below
  var dLon = deg2rad(lon2-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2)  
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2)
    ; 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; // Distance in km
  return d;
}

double deg2rad(deg) {
  return deg * (Math.pi/180);
}
Output:
1139.9231530436646

Source Calculate distance between two latitude-longitude points? (Haversine formula) credits to Chuck and @cmd_prompter.

Option 2

You can use https://pub.dev/packages/geolocator plugin if you are looking for the shortest distance between two locations aka LatLng.

double distanceInMeters = 
  await Geolocator().distanceBetween(52.2165157, 6.9437819, 52.3546274, 4.8285838);
  • Related