vectorToLocation static method

FlightLocation? vectorToLocation({
  1. required FlightLocation droneLocation,
  2. required FlightLocation pointOfInterest,
  3. required FlightVector vector,
})

Implementation

static FlightLocation? vectorToLocation(
    {required FlightLocation droneLocation,
    required FlightLocation pointOfInterest,
    required FlightVector vector}) {
  if (droneLocation.latitude == pointOfInterest.latitude &&
      droneLocation.longitude == pointOfInterest.longitude) {
    developer.log(
      'vectorToLocation - Waypoint Mission Point of Interest cannot be identical to the drone (home) location',
      name: kLogKindDjiFlutterPlugin,
    );
    return null;
  }

  final double alpha = (atan(
          (droneLocation.longitude - pointOfInterest.longitude) /
              (droneLocation.latitude - pointOfInterest.latitude)) *
      180 /
      pi);

  final double azimuth;
  if (droneLocation.latitude - pointOfInterest.latitude < 0) {
    // Drone is south to the Point of Interest
    // We need to add 180 to the azimuth (to compensate for the negative angle)
    azimuth = (alpha + 180) % 360;
  } else {
    azimuth = alpha % 360;
  }

  final double azimuthToDestination =
      (azimuth + vector.headingRelativeToPointOfInterest) % 360;

  // Latitude = North/South
  double computedDestinationLatitude = pointOfInterest.latitude +
      (vector.distanceFromPointOfInterest *
          sin((90 - azimuthToDestination) * pi / 180) *
          meterToDecimalDegree);
  // Setting the Latitude precision to 8 decimals (~1.1mm accuracy, which is the GPS limit).
  computedDestinationLatitude = computedDestinationLatitude * 100000000;
  final double destinationLatitude =
      computedDestinationLatitude.round() / 100000000;

  // Longitude = East/West
  double computedDestinationLongitude = pointOfInterest.longitude +
      (vector.distanceFromPointOfInterest *
          cos((90 - azimuthToDestination) * pi / 180) *
          meterToDecimalDegree);
  // Setting the Latitude precision to 8 decimals (~1.1mm accuracy, which is the GPS limit).
  computedDestinationLongitude = computedDestinationLongitude * 100000000;
  final double destinationLongitude =
      computedDestinationLongitude.round() / 100000000;

  developer.log(
    'vectorToLocation - computed coordinates:',
    name: kLogKindDjiFlutterPlugin,
  );
  developer.log(
    'Latitude: $destinationLatitude',
    name: kLogKindDjiFlutterPlugin,
  );
  developer.log(
    'Longitude: $destinationLongitude',
    name: kLogKindDjiFlutterPlugin,
  );
  developer.log(
    'Altitude: ${vector.destinationAltitude}',
    name: kLogKindDjiFlutterPlugin,
  );

  return FlightLocation(
      latitude: destinationLatitude,
      longitude: destinationLongitude,
      altitude: vector.destinationAltitude);
}