2011-02-23 31 views
5

Harita üzerinde bir dizi ek açıklama içeren bir başvuru için bir pusula geliştirmeye çalışıyorum. Bir açıklama seçebilmeyi ve pusulayı kullanıcının seçilen yere yönlendirmesini istiyorum.Pusula kullanarak konuma gelin

Kullanıcının bulunduğu konumdan, açıklama notunun bulunduğu yere kadar olan değeri hesapladım ve manyetik başlığa ve iPhone'un gerçek başlığına sahibim. Ayrıca bir görüntüyü nasıl döndüreceğimi biliyorum. Ama bir sonraki adımın ne olduğunu anlayamıyorum. kullanıcının konumu ve ek açıklama konumu arasındaki

derece böyle hesaplanır:

// 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; 
    } 

doğru ve manyetik yön böyle alınır:

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

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

kimse bana ne ben söyleyebilir iPhone döndürülse bile, ok işaretini konumlandırmak için şimdi yapmalı mıyım?

cevap

8

Yine bununla oynamak için zamanım oldu.

Bu yapacağım:

- (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

bu benim projeye deneyeceğim ve ben bunu gayet iyi çalışıyor umut .. harika .. sen benim gün :) –

+0

bana tam sınıfını gönderebilir kaydedilmiş Bunun için :) –

+0

evet, bu bilgi gerçekten yararlıdır. Bununla ilgili soruma cevap verebiliyor musunuz? - http://stackoverflow.com/questions/7173925/how-to-rotate-a-direction-arrow-using-clheading – Dhawal