d0n*_*n13 5 iphone filter moving-average mkmapview compass-geolocation
我正在使用罗盘标题来旋转MKMapView.旋转是有点生涩,所以我试图像iPhone上的谷歌地图那样过滤它(或者似乎做了一些诡计).
我试图使用移动平均公式来过滤iphone指南针的读数,但它在359和0之间的交叉处失败,因为它从35x开始向后平均到0并导致地图向后旋转,因为它从西方.
任何想法最好的方法是过滤这些数据,使其从359回到零,并保持滚动平均值.
代码在这里:
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
static float xd=0;
static float k = 0.22;
// Moving average formula
xd = k * xd + (1.0 - k) * newHeading.magneticHeading;
NSLog(@"%0.2f : %0.2f", newHeading.magneticHeading, xd);
[map setTransform:CGAffineTransformMakeRotation((-1 * xd * M_PI) /180)];}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
如果之前的移动平均线和新的方向相差超过 180 度,则以较小者为准加上 360。然后在存储新的移动平均线时除以 360。所以(没有精确的数学):
HDG MA
350 350
355 353
0 356 (because 353 - 0 > 180 so adjusted HDG is 360)
5 359 (likewise)
10 2 (likewise, then 362 is new MA, mod 360 to normalize)
350 356 (because 2 - 350 < -180 so adjusted MA is 362)
Run Code Online (Sandbox Code Playgroud)
我希望这种方法可行,并且比平均角度中描述的三角方法更有效(感谢 Mark Ransom 的引用)。