Point to location using compass

前端 未结 1 793
刺人心
刺人心 2020-12-05 16:46

I am trying to develop a compass for an appliation which has a set of annotations on a map. I would like to be able to choose an annotation and then have the compass point t

相关标签:
1条回答
  • 2020-12-05 17:27

    I had time to play with this again.

    This will do it:

    - (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 讨论(0)
提交回复
热议问题