Dart GPS轨迹纠偏算法
2020-03-10 本文已影响0人
动哒APP
功能描述
因项目需求,需要时刻跟踪用户当前位置。定位成功,手机静止不动的情况下,定位也会跳来跳去,总在漂移。
附上Dart代码:
import 'dart:math';
import 'package:amap_all_fluttify/amap_all_fluttify.dart';
/// <summary>
/// 纠偏
/// Rectify deviation
/// GPS纠偏算法,适用于google,高德体系的地图
/// </summary>
class Rectification {
static const double Pi = 3.14159265358979324;
static const double A = 6378245.0;
static const double Ee = 0.00669342162296594323;
static LatLng transform(double wgLat, double wgLon) {
if (outOfChina(wgLat, wgLon)) {
LatLng latlng = LatLng(wgLat, wgLon);
return latlng;
}
double dLat = _transformLat(wgLon - 105.0, wgLat - 35.0);
double dLon = _transformLon(wgLon - 105.0, wgLat - 35.0);
double radLat = wgLat / 180.0 * Pi;
double magic = sin(radLat);
magic = 1 - Ee * magic * magic;
double sqrtMagic = sqrt(magic);
dLat = (dLat * 180.0) / ((A * (1 - Ee)) / (magic * sqrtMagic) * Pi);
dLon = (dLon * 180.0) / (A / sqrtMagic * cos(radLat) * Pi);
LatLng latlng = LatLng(wgLat + dLat, wgLon + dLon);
return latlng;
}
static bool outOfChina(double lat, double lon) {
if (lon < 72.004 || lon > 137.8347) return true;
if (lat < 0.8293 || lat > 55.8271) return true;
return false;
}
static double _transformLat(double x, double y) {
double ret = -100.0 +
2.0 * x +
3.0 * y +
0.2 * y * y +
0.1 * x * y +
0.2 * sqrt(x.abs());
ret += (20.0 * sin(6.0 * x * Pi) + 20.0 * sin(2.0 * x * Pi)) * 2.0 / 3.0;
ret += (20.0 * sin(y * Pi) + 40.0 * sin(y / 3.0 * Pi)) * 2.0 / 3.0;
ret += (160.0 * sin(y / 12.0 * Pi) + 320 * sin(y * Pi / 30.0)) * 2.0 / 3.0;
return ret;
}
static double _transformLon(double x, double y) {
double ret =
300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(x.abs());
ret += (20.0 * sin(6.0 * x * Pi) + 20.0 * sin(2.0 * x * Pi)) * 2.0 / 3.0;
ret += (20.0 * sin(x * Pi) + 40.0 * sin(x / 3.0 * Pi)) * 2.0 / 3.0;
ret +=
(150.0 * sin(x / 12.0 * Pi) + 300.0 * sin(x / 30.0 * Pi)) * 2.0 / 3.0;
return ret;
}
}
原理我也在研究中,有懂的同学不妨留言。
附上GitHub地址:https://github.com/fluttify-project/amap_map_fluttify/issues/187