기기 제목에 따라 관심 장소 (POI)를 표시해야하는 iOS 앱을 개발 중입니다. 나는 CLLocationManager
을 사용하여 사용자의 위치와 제목을 얻었습니다. 한 쌍의 목적지 좌표가 있습니다. 이것을 바탕으로 나는 어느 분기가 그 것인지를 계산하고 남 (0도)도에서의 편차의 부동 값을도 단위로 반환합니다. 나는 북쪽에는 +/- 180도, 남쪽에는 0도있다. 다음은 코드 스 니펫입니다.두 벡터 사이의 각도 계산?
-(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 ;
}
어쨌든 나는 목표가 4 분기에 있지만, 남쪽에서 -93 도인 상황이 있습니다. 나는 길을 잃었고 그 문제를 해결하는 방법을 모른다. ...
편집 : (분기는 + y가 북쪽이고 + x가 동쪽 등등)
p.s .: iPhone의 나침반이 정말 나쁘다는 것을 읽었습니다. 그렇다면 Google지도와 같은 앱이 제대로 작동하고 있습니까?
편집 2 : 각도로 실수했습니다. 과격하게 o에는 동쪽에 -90 도가 있고 서쪽에 90도있다.
사이드 노트 : 부동 소수점을 평등하게 테스트하면 원하는 결과를 얻지 못할 수도 있습니다. – John
네, 정확하게 N/S/E/W를보고있는 것은 장치 정확도가 거의 없기 때문에 "수학적"목적으로 사용했습니다. – raistlin