distance static method

double distance(
  1. GeoCoordinate coordinate1,
  2. GeoCoordinate coordinate2
)

Get distance between coordinates in meters

Implementation

static double distance(GeoCoordinate coordinate1,GeoCoordinate coordinate2){
  double latitudeDistance = _2dDistance(point1: _toCoordinates(angle: coordinate1.latitude,altitude: coordinate1.altitude), point2: _toCoordinates(angle: coordinate2.latitude,altitude: coordinate2.altitude));
  double longitudeDistance = _2dDistance(point1: _toCoordinates(angle: coordinate1.longitude, altitude: coordinate1.altitude), point2: _toCoordinates(angle: coordinate2.longitude, altitude: coordinate2.altitude));
  double altitudeDistance = (coordinate2.altitude - coordinate1.altitude).abs();
  //Subtract the complete vector if the delta of the angles is bigger than 180 degrees
    //Do not do this for the latitude because it is always in a range of -90 to 90 degrees
  double deltaLongitude = (coordinate1.longitude - coordinate2.longitude).abs();
  //Calculate and subtract the complete vector to measure the distance from the other side of the globe
  if(180 < deltaLongitude){
    double completeVector = _2dDistance(point1: _toCoordinates(angle: 180, altitude: coordinate1.altitude), point2: _toCoordinates(angle: -180, altitude: coordinate2.altitude));
    //print('Complete vector: $completeVector longitudeDistance: $longitudeDistance');
    longitudeDistance = completeVector - longitudeDistance;
  }
  //Calculate the total distance by merging all of the vectors together
    //Merge distances in the x,y and z axis
  double absoluteDistance = pow(latitudeDistance, 2) + pow(longitudeDistance, 2) + (pow(altitudeDistance, 2) as double);
  //find the square root
  absoluteDistance = sqrt(absoluteDistance);
  return absoluteDistance;
}