how to find distance and time calculation between two location in flutter - flutter

I have multiple locations and current location, i want to find distance between my current location and other given location of google map flutter.
please provide any method or library which can easily calculate time and distance.I am Using this code but i can not find correct distance.
double _coordinateDistance(lat1, lon1, lat2, lon2) {
var p = 0.017453292519943295;
var c = cos;
var a = 0.5 - c((lat2 - lat1) * p) / 2 +
c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p)) / 2;
return 12742 * asin(sqrt(a));}

use https://pub.dev/packages/flutter_locations_distance package
add your multiple locations into lat and lon array and use this code
for(int i=0;i<locCount;i++){
double distance;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
distance = await FlutterLocationsDistance().distanceBetween(currentLat,
currentLon, lat[i], lon[i]);
} on PlatformException {
distance = -1.0;
}
}
and for each multiple locations you can get the distance between current location and other locations.
to get your current location use https://pub.dev/packages/location package.
for the time you can use https://pub.dev/packages/stop_watch_timer package

Related

Check if 2 coordinates are in the same city

Basically, I have 2 LatLong values 1 for the user that keeps updating according to his current location and 1 stored on the database. How can I check if these 2 coordinates are in the same city or state.
Note: I used the google maps API https://maps.googleapis.com/maps/api/geocode/json?latlng=33.26944346171845,%2035.20463784635442&sensor=true&key=KEY for both coordinates and compared strings(administrative_area_level_2) to check if the user is in the same city or not. Is there a better way to do it in Flutter?
Thanks in advance!
I think your method is quite reasonable. If you don't want to use API, you can calculate the distance between two coordinates with the below method. Using this, you can assume that those below a certain distance are in the same city or state.
const convertToRadian = x => x * Math.PI / 180;
function getDistanceBetween(lat1, lat2, lng1, lng2) {
const R = 6378137; // Earth’s mean radius in meter
const dLat = convertToRadian(lat2 - lat1);
const dLong = convertToRadian(lng2 - lng1);
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(convertToRadian(lat1)) * Math.cos(convertToRadian(lat2)) * Math.sin(dLong / 2) * Math.sin(dLong / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c; // returns the distance in meter
}

is there any formula to get travel time between two locations or from distance in flutter without using google maps

I have two locations and I have the formula to get the distance between them but I want to calculate the travel time between them without using google maps or any API calling. Is there any formula to do that in flutter?
I think the real travel time depends on a lot of factors, for example the road you take or the speed limit.
Whitout some complex elaboration, which are usually perfomed by a backend, I don't think it is possible to calculate it.
Anyway, since you have got the distance, you could simply estimate it by giving a supposed speed and then using basic physics formulas.
time = distance / speed.
You cannot find the travel distance between two locations without using the Google API.
However you can get the air distance in meters using this formula in flutter.
double calculateDistance(
double lat1, double lon1, double lat2, double lon2) {
var p = 0.017453292519943295;
var c = cos;
var a = 0.5 -
c((lat2 - lat1) * p) / 2 +
c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p)) / 2;
var temp = 12742 * asin(sqrt(a));
return (temp * 1000);
}

how to calculate distance between two location on Flutter?(result should be meters)

How to calculate distance between two location in dart?
Without using plugin
double calculateDistance(lat1, lon1, lat2, lon2){
var p = 0.017453292519943295;
var c = cos;
var a = 0.5 - c((lat2 - lat1) * p)/2 +
c(lat1 * p) * c(lat2 * p) *
(1 - c((lon2 - lon1) * p))/2;
return 12742 * asin(sqrt(a));
}
result is KM. if you want meter just multiply by 1000. return 1000 * 12742 * asin(sqrt(a))
You can use geolocator plugin to calculate to distances between two coordinates:
An Example:
var _distanceInMeters = await Geolocator().distanceBetween(
_latitudeForCalculation,
_longitudeForCalculation,
_currentPosition.latitude,
_currentPosition.longitude,
);
Check out GeoLocator Plugin for more info.
Check out latlong. It's a package dedicated to work with coordinates and features sophisticated calculations.
Unlike packages like geolocator it does not come with integrated location services and can be used alongside packages specialized for that.
You can use latlong2 which is the continuation of latlong. It has features like distance calculation and path drawing. So you can get distance as path distance as well as direct distance.

Search nearest objects from firebase in flutter

Good day,
I want to do findapp in flutter.
Help please with decide calculate distance between points.
I found this code
It decision for two points.
But I need use circle - in my firebase coordinates of many object - about 200. I want show to user 3 nearest objects from these. How I need to do it correct?
Thank You
import 'dart:math' show cos, sqrt, asin;
void main() {
double calculateDistance(lat1, lon1, lat2, lon2){
var p = 0.017453292519943295;
var c = cos;
var a = 0.5 - c((lat2 - lat1) * p)/2 +
c(lat1 * p) * c(lat2 * p) *
(1 - c((lon2 - lon1) * p))/2;
return 12742 * asin(sqrt(a));
}
double totalDistance = calculateDistance(26.196435, 78.197535,26.197195, 78.196408);
print(totalDistance);
}
You can use the method from the geolocator package:
Like this:
import 'package:geolocator/geolocator.dart';
double distanceInMeters = await Geolocator().distanceBetween(52.2165157, 6.9437819, 52.3546274, 4.8285838);
Reference:
https://pub.dev/packages/geolocator#calculate-distance

iPhone: calculate distance while walking using GPS

I want to calculate the distance that users cover while walking using GPS. For example a user taps the start button and starts to walk or run than when he done he taps stop. What will be the minimum distance user has to travel to get the different lat long?
How can we do it in IPhone, asume we take Lat, long after every 0.3 sec than in the last we have a list of points?
You could do this by calculating the distance between 2 points (latitude, longitude):
(I haven't tested it):
-(double)distanceBetweenCoordinate:(CLLocationCoordinate2D)c1 andCoordinate:(CLLocationCoordinate2D)c2 {
double long1 = degreesToRadians(c1.longitude);
double lat1 = degreesToRadians(90 - c1.latitude);
double long2 = degreesToRadians(c2.longitude);
double lat2 = degreesToRadians(90 - c2.latitude);
double gamma = fabs(long1 - long2);
if (gamma > M_PI) {
gamma = 2 * M_PI - gamma;
}
double result = cos(lat2) * cos(lat1) + sin(lat2) * sin(lat1) * cos(gamma);
return acos(result) * 6366.1977; // Kilometers
};
CGFloat degreesToRadians(CGFloat degrees) {
return degrees * M_PI / 180;
};
UPDATE: Use distanceFromLocation - Calculate distance between two points instead