2011-02-23 99 views
5

我正在嘗試開發一個應用程序的指南針,它在地圖上有一組註釋。我希望能夠選擇註釋,然後讓指南針將用戶指向所選位置。指向使用指南針的位置

我已經計算了從用戶位置到註釋位置的degress,並且我擁有了iPhone的磁性標題和真正的標題。我也知道如何旋轉圖像。 但我無法弄清楚下一步是什麼。

用戶的位置,並標註位置之間的度計算如下:

// get user location 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation]; 
    [locationManager startUpdatingHeading]; 
    CLLocation *location = [locationManager location]; 
    CLLocationCoordinate2D coordinate = [location coordinate]; 

    float x1 = coordinate.latitude; 
    float y1 = coordinate.longitude; 
    float x2 = [annLatitude floatValue]; 
    float y2 = [annLongitude floatValue]; 

    float dx = (x2 - x1); 
    float dy = (y2 - y1); 

    if (dx == 0) { 
     if (dy > 0) { 
      result = 90; 
     } 
     else { 
      result = 270; 
     } 
    } 
    else { 
     result = (atan(dy/dx)) * 180/M_PI; 
    } 

    if (dx < 0) { 
     result = result + 180; 
    } 

    if (result < 0) { 
     result = result + 360; 
    } 

真與磁航向檢索這樣的:

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { 
    arrow.transform = CGAffineTransformMakeRotation(newHeading.magneticHeading); 

    // NSLog(@"magnetic heading: %f", newHeading.magneticHeading); 
    // NSLog(@"true heading: %f", newHeading.trueHeading); 
} 

誰能告訴我是什麼現在應該做,使箭頭指向的位置 - 即使iPhone旋轉?

回答

8

我有時間再玩一次。

這將做到這一點:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation]; 
    [locationManager startUpdatingHeading]; 

    CLLocation *location = [locationManager location]; 
    CLLocationCoordinate2D user = [location coordinate]; 

    [self calculateUserAngle:user]; 
} 

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    CLLocationCoordinate2D here = newLocation.coordinate; 

    [self calculateUserAngle:here]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { 
    compass.transform = CGAffineTransformMakeRotation(newHeading.magneticHeading * M_PI/180); 
    needle.transform = CGAffineTransformMakeRotation((degrees - newHeading.magneticHeading) * M_PI/180); 
} 

-(void) calculateUserAngle:(CLLocationCoordinate2D)user { 
    locLat = [[targetLocationDictionary objectForKey:@"latitude"] floatValue]; 
    locLon = [[targetLocationDictionary objectForKey:@"longitude"] floatValue]; 

    NSLog(@"%f ; %f", locLat, locLon); 

    float pLat; 
    float pLon; 

    if(locLat > user.latitude && locLon > user.longitude) { 
     // north east 

     pLat = user.latitude; 
     pLon = locLon; 

     degrees = 0; 
    } 
    else if(locLat > user.latitude && locLon < user.longitude) { 
     // south east 

     pLat = locLat; 
     pLon = user.longitude; 

     degrees = 45; 
    } 
    else if(locLat < user.latitude && locLon < user.longitude) { 
     // south west 

     pLat = locLat; 
     pLon = user.latitude; 

     degrees = 180; 
    } 
    else if(locLat < user.latitude && locLon > user.longitude) { 
     // north west 

     pLat = locLat; 
     pLon = user.longitude; 

     degrees = 225; 
    } 

    // Vector QP (from user to point) 
    float vQPlat = pLat - user.latitude; 
    float vQPlon = pLon - user.longitude; 

    // Vector QL (from user to location) 
    float vQLlat = locLat - user.latitude; 
    float vQLlon = locLon - user.longitude; 

    // degrees between QP and QL 
    float cosDegrees = (vQPlat * vQLlat + vQPlon * vQLlon)/sqrt((vQPlat*vQPlat + vQPlon*vQPlon) * (vQLlat*vQLlat + vQLlon*vQLlon)); 
    degrees = degrees + acos(cosDegrees); 
} 
+0

這是偉大的..我要把它嘗試我的項目,我希望它工作得很好..你救了我的一天:) – 2011-05-06 18:24:12

+0

你能送我完整的類爲此:) – 2011-05-07 12:16:34

+0

是的,這些信息真的很有幫助。你能回答我的問題嗎? - http://stackoverflow.com/questions/7173925/how-to-rotate-a-direction-arrow-using-clheading – Dhawal 2011-08-25 05:00:46