Skip to content

Instantly share code, notes, and snippets.

@hotdang-ca
Created November 24, 2021 16:48
Show Gist options
  • Save hotdang-ca/51838776fa447df54d797b957ed137e1 to your computer and use it in GitHub Desktop.
Save hotdang-ca/51838776fa447df54d797b957ed137e1 to your computer and use it in GitHub Desktop.
Dart code to calculate distance between two lat/lng decimal coordinates
import 'dart:math';
/// Describes a distance unit for conversion
enum Unit {
miles,
kilometers,
nauticalMiles,
}
/// Describes a decimal coordinate
class Coordinate {
num latitude;
num longitude;
String name;
Coordinate({this.name, this.latitude, this.longitude});
}
/// This routine calculates the distance between two points (given the
/// latitude/longitude of those points). It is based on free code used to
/// calculate the distance between two locations using GeoDataSource(TM)
/// products.
///
/// Definitions:
/// South latitudes are negative, east longitudes are positive
///
/// Passed to function:
/// Coordinate a = Latitude and Longitude of point 1 (in decimal degrees)
/// Coordinate b = Latitude and Longitude of point 2 (in decimal degrees)
/// optional: Unit = the unit you desire for results
/// where: 'Unit.miles' is statute miles (default, or omitted)
/// 'Unit.kilometers' is kilometers
/// 'Unit.nauticalMiles' is nautical miles
///
/// Worldwide cities and other features databases with latitude longitude
/// are available at https://www.geodatasource.com
///
/// For enquiries, please contact sales@geodatasource.com
///
/// Official Web site: https://www.geodatasource.com
///
/// Dart code James Robert Perih (c) All Rights Reserved 2021
///
/// GeoDataSource.com (C) All Rights Reserved 2017
///
///
num distanceBetweenTwoPoints(
final Coordinate a,
final Coordinate b,
final Unit units,
) {
final num radlat1 = pi * a.latitude / 180.0;
final num radlat2 = pi * b.latitude / 180.0;
final num theta = a.longitude - b.longitude;
final num radtheta = pi * theta / 180.0;
num distance = sin(radlat1) * sin(radlat2) + cos(radlat1) * cos(radlat2) * cos(radtheta);
if (distance > 1) {
distance = 1;
}
distance = acos(distance);
distance = distance * 180.0 / pi;
distance = distance * 60 * 1.1515;
switch (units) {
case Unit.kilometers:
distance = distance * 1.609344;
break;
case Unit.nauticalMiles:
distance = distance * 0.8684;
break;
case Unit.miles:
default:
// intentionally left blank
}
return distance;
}
/// test implementation
void main() {
Coordinate winnipeg = Coordinate(name: 'Winnipeg', latitude: 49.895077, longitude: -97.138451);
Coordinate regina = Coordinate(name: 'Regina', latitude: 50.445210, longitude: -104.618896);
Coordinate calgary = Coordinate(name: 'Calgary', latitude: 51.04369632775411, longitude: -114.04254321432143);
print('Distance between ${winnipeg.name} & ${regina.name}: ${distanceBetweenTwoPoints(winnipeg, regina, Unit.kilometers)}');
print('Distance between ${regina.name} & ${calgary.name}: ${distanceBetweenTwoPoints(regina, calgary, Unit.kilometers)}');
print('Distance between ${winnipeg.name} & ${calgary.name}: ${distanceBetweenTwoPoints(winnipeg, calgary, Unit.kilometers)}');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment