Reputation: 21
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.
Upvotes: 2
Views: 8588
Reputation: 1364
This is the url you used to call the api just pass Your Google Api Key and start end latitude and longitude value.
'https://maps.googleapis.com/maps/api/distancematrix/json?destinations=40.659569,-73.933783&origins=40.6655101,-73.89188969999998&key=YOUR_API_KEY_HERE'
import 'package:http/http.dart' as http;
static Future<dynamic> getDistance({double startLatitude, double startLongitude, double endLatitude, double endLongitude}) async {
String Url = 'https://maps.googleapis.com/maps/api/distancematrix/json?destinations=${startLatitude},${startLongitude}&origins=${endLatitude},${endLongitude}&key=YOUR_API_KEY-HERE';
try {
var response = await http.get(
Uri.parse(Url),);
if (response.statusCode == 200) {
return jsonDecode(response.body);
} else
return null;
}
catch (e) {
print(e);
return null;
}
}
Call the function
getDistance(
startLatitude: '52.2165157',
startLongitude: '6.9437819',
endLatitude: '52.3546274',
endLongitude: '4.8285838'
);
Upvotes: 0
Reputation: 21
import 'package:dio/dio.dart';
Dio _dio = new Dio();
Response response = await _dio.get(
"https://maps.googleapis.com/maps/api/distancematrix/json?destinations=$dlatitude,$dlongitude&origins=$slatitude,$slongitude&key=YOUR_KEY_HERE");
print(response.data);
Note: the slatitude,dlatitude, slongitude and dlongitude is the dynamic value.
you can also update them as:
String dlatitude = destinationLocation!.latitude.toString();
String dlongitude = destinationLocation!.longitude.toString();
String slatitude = startLocation.latitude.toString();
String slongitude = startLocation.longitude.toString();
Hope this helps you. :)
Upvotes: 2
Reputation: 1486
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.
The content of all the links in this answer may change in the future
Upvotes: 5
Reputation: 1052
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);
Upvotes: -1