getMetersPerLongitudeDegAtLatitudeDeg function Null safety

double getMetersPerLongitudeDegAtLatitudeDeg(
  1. dynamic latitudeDeg
)

Implementation

double getMetersPerLongitudeDegAtLatitudeDeg(latitudeDeg) {
  // Slice the earth in cylinders of 1 degree latitude, where the radius of
  // each cylinder is taken equal to the radius at the bottom of the cylinder
  // (assuming the Northern hemisphere).
  // This will overestimate the radius at the top of the cylinder.

  // Symmetry in the equator -> only deal with positive angles.
  var i = latitudeDeg.truncate().abs();

  // Prevent index out of bounds for the case of abs(latitude)==90 deg.
  i = i < 90 ? i : 89;

  // We may have a memoized value.
  var res = _metersPerLongitudeDegreeAtLatitude[i];

  if (res == null) {
    // No memoized value -> calculate and memoize.

    // With the earth being symmetric at the equator, only work in the
    // positive angles domain.
    final latitudeRad = degToRad(latitudeDeg.abs());

    // Radius of the parallel at the specified latitude.
    final radiusOfParallel = EarthRadiusMeters.mean * cos(latitudeRad);

    // Calculate how many meters is one degree of latitude.
    final circumferenceOfParallel = radiusOfParallel * 2 * pi;
    res = circumferenceOfParallel / 360;

    // Memoize.
    _metersPerLongitudeDegreeAtLatitude[i] = res;
  }
  return res;
}