yeradis / haversine.dart

Calculating geographic distances using the Haversine formula in Dart
MIT License
5 stars 8 forks source link

Rename constant PI to pi #3

Open wil93 opened 5 years ago

wil93 commented 5 years ago

Apparently Dart has renamed the PI constant in 'dart:math', it is now named pi.

See: https://api.dart.dev/stable/2.4.0/dart-math/pi-constant.html

When using haversine package I am getting this error:

Compiler message:
file:///C:/flutter/.pub-cache/hosted/pub.dartlang.org/haversine-1.0.2/lib/src/haversine_base.dart:72:68: Error: Getter not found: 'PI'.
    double _radiansFromDegrees(final double degrees) => degrees * (PI / 180.0);
                                                                   ^^
file:///C:/flutter/.pub-cache/hosted/pub.dartlang.org/haversine-1.0.2/lib/src/haversine_base.dart:72:68: Error: The getter 'PI' isn't defined for the class 'Haversine'.
 - 'Haversine' is from 'package:haversine/src/haversine_base.dart' ('file:///C:/flutter/.pub-cache/hosted/pub.dartlang.org/haversine-1.0.2/lib/src/haversine_base.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'PI'.
    double _radiansFromDegrees(final double degrees) => degrees * (PI / 180.0);
                                                                   ^^
scrlkx commented 4 years ago

There are updates about this?

wil93 commented 4 years ago

In my case, I just used a custom class for this:

class GreatCircleDistance {
  final double R = 6371000;  // radius of Earth, in meters
  double latitude1, longitude1;
  double latitude2, longitude2;

  GreatCircleDistance({this.latitude1, this.latitude2, this.longitude1, this.longitude2});

  double distance() {
    double phi1 = this.latitude1 * pi / 180;  // φ1
    double phi2 = this.latitude2 * pi / 180;  // φ2
    var deltaPhi = (this.latitude2 - this.latitude1) * pi / 180;  // Δφ
    var deltaLambda = (this.longitude2 - this.longitude1) * pi / 180;  // Δλ

    var a = sin(deltaPhi / 2) * sin(deltaPhi / 2) +
            cos(phi1) * cos(phi2) * sin(deltaLambda / 2) * sin(deltaLambda / 2);
    var c = 2 * atan2(sqrt(a), sqrt(1 - a));

    return R * c;
  }
}

See https://github.com/wil93/ranky/blob/master/lib/data.dart#L86

scrlkx commented 4 years ago

This looks good. Thank you for that.