关于用 Dart 编程

About Coding with Dart

我想将 Lat Lon 坐标转换为 UTM。我写了一段代码,dart pad 中没有弹出任何问题。但是在 运行 代码上,我收到一个错误:

Lat: 27.719773366 Lon: 84.0979457557 Uncaught TypeError: C.JSNull_methods.$add is not a function

import 'dart:math' as math;

void main() {
  Location locate = Location();
  locate.getLocation();
  double lat = locate.latitude;
  double lon = locate.longitude;
  LatLonUTM utmco = LatLonUTM(lat: lat,lon: lon);
  String utm = utmco.converter();
  print(utm);
}

class Location {
  double latitude;
  double longitude;
  getLocation() {
    latitude = 27.719773366;
    longitude = 84.0979457557;
    print('Lat: $latitude Lon: $longitude');
  }
}

class LatLonUTM {
  double lat;
  double lon;
  LatLonUTM({this.lat, this.lon});
  String converter() {
    final fe = 500000;
    final a = 6378137;
    final f = 1 / 298.257223563;
    final lt = toRadians(lat);
    final zn = ((lon + 180) / 6).floor() + 1;
    final l0 = toRadians((zn - 1) * 6 - 180 + 3);
    final ln = toRadians(lon) - l0;
    final k0 = 0.9996;
    final e = math.sqrt(f * (2 - f));
    final n = f / (2 - f);
    final n2 = n * n;
    final n3 = n2 * n;
    final n4 = n3 * n;
    final n5 = n4 * n;
    final n6 = n5 * n;
    final t = math.tan(lt);
    final sg = sinh(e * atanh(e * (t / math.sqrt(1 + t * t))));
    final tp = t * math.sqrt(1 + sg * sg) - sg * math.sqrt(1 + t * t);
    final sip = math.atan2(tp, math.cos(ln));
    final etap =
        asinh(math.sin(ln) / math.sqrt(tp * tp + math.cos(ln) * math.cos(ln)));
    final A = a / (1 + n) * (1 + 1 / 4 * n2 + 1 / 64 * n4 + 1 / 256 * n6);
    List<double> ar = [
      0,
      (1 / 2 * n -
          2 / 3 * n2 +
          5 / 16 * n3 +
          41 / 180 * n4 -
          127 / 288 * n5 +
          7891 / 37800 * n6),
      (13 / 48 * n2 -
          3 / 5 * n3 +
          557 / 1440 * n4 +
          281 / 630 * n5 -
          1983433 / 1935360 * n6),
      (61 / 240 * n3 -
          103 / 140 * n4 +
          15061 / 26880 * n5 +
          167603 / 181440 * n6),
      (49561 / 161280 * n4 - 179 / 168 * n5 + 6601661 / 7257600 * n6),
      (34729 / 80640 * n5 - 3418889 / 1995840 * n6),
      (212378941 / 319334400 * n6)
    ];

      double sum1;
      for (int j = 1; j <= 6; j++)
        sum1 += ar[j] * math.sin(2 * j * sip) * cosh(2 * j * etap);
      double sum2;
      for (int j = 1; j <= 6; j++)
        sum2 += ar[j] * math.cos(2 * j * sip) * sinh(2 * j * etap);

    final si = sip + sum1;
    final eta = etap + sum2;
    final x = k0 * A * eta;
    final y = k0 * A * si;
    final x0 = x + fe;
    final y0 = y < 0 ? y + 1000000 : y;
    final xco = x0.toStringAsPrecision(9);
    final yco = y0.toStringAsPrecision(9);
    final zl = lon < 0 ? 'S' : 'N';
    String utm = zn.toString() + zl + xco + yco;
    return utm;
  }
}

double toRadians(angle) {
  double rad;
  rad = (angle * math.pi) / 180;
  return rad;
}

double sinh(double angle) {
  double sinhx;
  sinhx = (math.exp(angle) - math.exp(-angle)) / 2;
  return sinhx;
}

double cosh(double angle) {
  double coshx;
  coshx = (math.exp(angle) + math.exp(-angle)) / 2;
  return coshx;
}

double tanh(double angle) {
  double tanhx;
  tanhx = (math.exp(angle) - math.exp(-angle)) /
      (math.exp(angle) + math.exp(-angle));
  tanhx = angle >= 19 ? 1 : angle <= -19 ? -1 : tanhx;
  return tanhx;
}

double atanh(double angle) {
  double atanhx;
  atanhx = 0.5 * (math.log(1 + angle) / math.log(1 - angle));
  return atanhx;
}

double asinh(double angle) {
  if (angle.abs() >= 268435456.0)
    return angle.sign * (math.log(angle.abs()) + math.log(2.0));

  return angle.sign * math.log(angle.abs() + math.sqrt((angle * angle) + 1));
}

尝试将 sum1sum2 初始化为零:

double sum1 = 0.0;
...
double sum2 = 0.0;