【发布时间】:2013-05-23 14:12:54
【问题描述】:
我正在开发一个 iOS 应用程序,它必须根据设备标题显示 POI(兴趣点)。我使用CLLocationManager 来获取用户的位置和航向。我有一对目的地的坐标。基于此,我正在计算哪个季度是那个季度,并以度为单位返回与南(0度)偏差的浮点值。我在北方有 -/+180 度,在南方有 0 度。这是代码sn-p:
-(float)updateTargetLongitude:(float)lon Latitude:(float)lat{
// //longitude = x
// //latitude = y
NSLog(@"current location = (%.5f, %.5f)", [[NSUserDefaults standardUserDefaults] doubleForKey:@"currentLongitude"], [[NSUserDefaults standardUserDefaults] doubleForKey:@"currentLatitude"]);
float x = lon - [[NSUserDefaults standardUserDefaults] doubleForKey:@"currentLongitude"];
float y = lat - [[NSUserDefaults standardUserDefaults] doubleForKey:@"currentLatitude"];
float angle;
NSLog(@"Searching angle from source (%.5f, %.5f) to destination (%.5f, %.5f)", locationManager.location.coordinate.longitude, locationManager.location.coordinate.latitude, lon, lat);
if (x == 0 && y == 0) {
NSLog(@"you're there already!");
return -0.1;
}
if(x == 0 && y > 0){
NSLog(@"look north");
angle = 180.0;
}
if (x == 0 && y < 0) {
NSLog(@"look south");
angle = 0;
}
if (x > 0 && y == 0) {
NSLog(@"look east");
angle = 90.0;
}
if (x < 0 && y == 0) {
NSLog(@"look west");
angle = -90;
}
if (x > 0 && y > 0) {
NSLog(@"first quarter");
angle = -atan2f(y, x) - M_PI_2;
}
if (x < 0 && y > 0) {
NSLog(@"second quarter");
angle = atan2f(y, x) + M_PI_2;
}
if (x < 0 && y < 0) {
NSLog(@"third quarter");
angle = atan2f(x, y);
}
if (x > 0 && y < 0) {
NSLog(@"fourth quarter");
angle = -atan2f(x, y);
}
NSLog(@"returning radians angle = %.4f for (%.5f, %.5f) :: degrees = %.3f", angle, y, x, angle * 180 / M_PI);
return angle * 180 / M_PI ;
}
不知何故,我的情况是,目标在第四节,但距离南方 -93 度。我迷路了,我不知道如何解决这个问题......
编辑:四分之一我的意思是笛卡尔坐标系,其中+y是北,+x是东等等!
p.s.:我听说 iPhone 指南针真的很糟糕,但如果是这样的话,谷歌地图之类的应用程序如何正常工作?
edit2:我在角度上犯了一个错误。官方官方说东-90度西90度。
【问题讨论】:
-
旁注:测试浮点是否相等可能不会给您预期的结果。
-
是的,但是准确地看 N/S/E/W 设备精度几乎是不可能的,所以这完全是出于“数学”目的
标签: iphone ios cllocationmanager compass-geolocation