Created
February 19, 2024 06:49
-
-
Save officialismailshah/d27ac5b070d11a42d88d4aa047eb9702 to your computer and use it in GitHub Desktop.
distance calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
void main() { | |
double distance = calculateDistance( | |
startLat: 25.285446, | |
startLong: 51.531040, | |
endLat: 25.285446, | |
endLong: 51.531040); | |
print(distance); | |
if (distance <= 5000) { | |
print("enable the function"); | |
} else { | |
print("disable the function"); | |
} | |
} | |
double calculateDistance( | |
{required double startLat, | |
required double startLong, | |
required double endLat, | |
required double endLong}) { | |
const double earthRadius = 6371; | |
double startLatRad = degreesToRadians(startLat); | |
double startLongRad = degreesToRadians(startLong); | |
double endLatRad = degreesToRadians(endLat); | |
double endLongRad = degreesToRadians(endLong); | |
double latDiff = endLatRad - startLatRad; | |
double longDiff = endLongRad - startLongRad; | |
// Calculate distance using Haversine formula | |
double a = pow(sin(latDiff / 2), 2) + | |
cos(startLatRad) * cos(endLatRad) * pow(sin(longDiff / 2), 2); | |
double c = 2 * atan2(sqrt(a), sqrt(1 - a)); | |
double distance = earthRadius * c; // Distance in kilometers | |
return distance; | |
} | |
// Function to convert degrees to radians | |
double degreesToRadians(double degrees) { | |
return degrees * pi / 180; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment