2011-09-04 3 views
4

나는 객관적으로 새로운 - C 그래서 내 질문은 너무 쓰레기면 사과하고 싶습니다.iPhone의 특정 위치에 나침반 표시

나침반을 프로그래밍 중이지만 아직 작동 중입니다 (튜토리얼 덕분에). 실제 위치 (경도 및 위도)가 표시됩니다. 나침반 내의 화살표가 특정 위치 (경도 2 및 경도 2)의 방향을 표시 할 수 있습니까? 나는 아이폰의 자성과 진정한 표제를 고려해야한다는 것을 읽었다.

+0

자습서 링크를 공유해 주실 수 있습니까? – Wasim

답변

2

너무 정확하지 않아도된다면 상당히 쉽습니다. 2 점 사이의 각도를 계산하고 나침반에 각도를 표시하기 만하면됩니다. (당신이 코드를 게시하지 않은, 당신은 setAngleForCompass 같은 방법이 있는지 확실하지 않습니다,하지만 당신은해야한다!)이보십시오 :

float dx = Longitude2 - Longitude1; 
float dy = Latitude2 - Latitude1; 
float angle = atan2(dy, dx); 
// Set the angle of the compass here. 

다른 방법으로, 자이로 스코프에 대한 액세스 권한을 얻기 위해 CMMotionManager을 사용할 수 있습니다 , 그것은 또한 올바른 방향으로 당신을 가리킬 것입니다. (말장난 의도!) 그 희망은 도움이됩니다!

+0

내 anser를 참조하십시오. – moonix82

1

위치에 대한 표제를 계산해야했습니다. 내가 찾은 가장 좋은 방법은 코사인의 구형 법칙 (Spherical Law of Cosines)을 사용하는 것이 었습니다. 이 기능을 구현하기 위해 C 함수를 만들었으며 사용할 수있는 것은 Here on github입니다. headingInDegrees 함수가 필요한 것 같습니다. 이것은 당신에게 진정한 표제를 줄 것입니다.

-1

감사의 말로 대답 해주세요. 나는 특정 방향을 보여주는 화살표를 만들고있다. 지금 내가 한 것은이 화살표로 애니메이션을 만드는 것입니다. 그러나 문제는 아이폰이 "방향"과 같은 방향을 가리켜 야한다는 것입니다. 지금까지

내 코드 :

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading 
{double heading = newHeading.trueHeading; 
[self rotateCompass:heading]; 
} 

- (void) rotateCompass: (double) degrees{ 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDuration:2]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
[UIView commitAnimations]; 
compassRose.transform = CGAffineTransformMakeRotation(degrees); 
} 

나는 나에게 제목을 보여주는 다른 위도, 경도를 얻을 수있는 루틴 및 라벨을 가지고 있고 그것을 작동합니다. 내 화살표가 아무 이유없이 열광합니다. 나는 화살표 방향을 trueHeading과 같은 방향으로하고 싶습니다. 아이폰이

+0

나는 이것이 "double heading = newHeading.magneticHeading''이어야한다고 생각한다. – confile

+0

xib의 화살표 이미지는 전화의 위쪽을 향해야한다. – Guy

3
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { 
    double heading = newHeading.trueHeading; //in degree relative to true north 
    double bearing = ... //get current bearing in degree relative to true north 
         //with the JS library mentioned below it would be something like 
         //bearing = userLoc.bearingTo(destionation); 

    //just two lines, the the sake of the example 
    double rotationInDegree = (bearing - heading); //the important line 
    rotationInDegree = fmod((rotationInDegree + 360), 360); //just to be on the safe side :-) 
    compassViewPerhapsAsImage.transform=CGAffineTransformMakeRotation(DegreesToRadians(rotationInDegree)); 
} 

좋은 링크 베어링 계산하는 방법으로 의미 :

0

로 : 이 당신에게 북극과 대상 사이의 천사를 제공 https://github.com/tadelv/CLLocation-Bearing

CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI/180;} 
CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180/M_PI;} 

- (void)calculating bearing { 
     float lat1 = DegreesToRadians(YOURCURRENTPOSITION.coordinate.latitude); 
     float lng1 = DegreesToRadians(YOURCURRENTPOSITION.coordinate.longitude); 
     float lat2 = DegreesToRadians(YOURTARGETPOSITION.coordinate.latitude); 
     float lng2 = DegreesToRadians(YOURTARGETPOSITION.coordinate.longitude); 
     float deltalng = lng2 - lng1; 
     double y = sin(deltalng) * cos(lat2); 
     double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(deltalng); 
     double bearing = atan2(y, x) + 2 * M_PI; 
     float bearingDegrees = RadiansToDegrees(bearing); 
     bearingDegrees = (int)bearingDegrees % 360; 

     NSLog(@"%d", (int)bearingDegrees); 
} 

. 그러면 현재 표제의 각도를 더하거나 뺄 필요가 있습니다.

관련 문제