2012-05-23 7 views
5

일부 iPhone 앱에서 작업 중이며 가속도계를 사용하는 사용자 위치에 따라 특정 지리적 위치로 이동하는 이미지 인 베어링을 만들고 싶습니다.iOS :지도가없는 현재 위치에서 위치 정보에 대한 각도 가져 오기

많은 답변을 읽었지만 해결책을 찾지 못했습니다.

나는 현재 위치 좌표와 목적지 좌표를 가지고 있습니다.

아이디어 나 샘플 코드가 있습니까? 감사.

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    GeoAngle = [self setLatLonForDistanceAndAngle:newLocation]; 
} 

그리고 다음과 같은 기능이있을 것 -을 :

+0

보기 [이 (http://stackoverflow.com/questions/6745131/how-can-we-find-the-angle-between-two-locations-defined-by-latitude-or-longitude) 그리고 [this] (http://www.movable-type.co.uk/scripts/latlong.html) 참조 –

답변

9

상단에 위치 관리자의 대리자 메서드에 .H 파일

float GeoAngle; 

#define RadiansToDegrees(radians)(radians * 180.0/M_PI) 
#define DegreesToRadians(degrees)(degrees * M_PI/180.0) 

정의합니다 변수를 정의 : -

-(float)setLatLonForDistanceAndAngle:(CLLocation *)userlocation 
{ 
    float lat1 = DegreesToRadians(userlocation.coordinate.latitude); 
    float lon1 = DegreesToRadians(userlocation.coordinate.longitude); 

    float lat2 = DegreesToRadians(locLat); 
    float lon2 = DegreesToRadians(locLon); 

    float dLon = lon2 - lon1; 

    float y = sin(dLon) * cos(lat2); 
    float x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon); 
    float radiansBearing = atan2(y, x); 
    if(radiansBearing < 0.0) 
    { 
     radiansBearing += 2*M_PI; 
    } 

    return radiansBearing; 
} 

GeoAngle 변수에서 각도가 계속 업데이트됩니다. 화살표를 대상 위치로 회전하려면 화살표 이미지를 사용하여 arrowImageView의 IBOutlet에 할당하고 머리글 업데이트 메서드에서이 옵션을 회전합니다.

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading 
{ 
    float direction = -newHeading.trueHeading; 

    arrowImageView.transform=CGAffineTransformMakeRotation((direction* M_PI/180)+ GeoAngle); 
} 
+0

에 대해 알고 싶습니다. 그렇지만이 방향을 보여주기 위해 가속도계를 사용하여 이미지를 올바르게 회전하는 방법은 무엇입니까? 나는이 방법으로 각도를 사용했다. - (void) 가속도계 : (UIAccelerometer *) 가속도계 가속도 : (UIAcceleration *) 가속도; 하지만 여전히 아이폰을 회전 업데이 트되지 않습니다. – hafedh

+0

화살표를 목적지 위치로 향하게하고 대답을 다시 확인하면 대답을 편집했습니다. – Dhawal

+0

효과가있었습니다! 고맙습니다. – hafedh