3

无论设备航向如何,我们如何才能指向特定的位置坐标。我尝试过Haversine 公式来计算两点之间的方位并将设备航向添加到该角度。但它仍然会产生问题。

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    degrees = [self bearingToLocation:newLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
    {
        float direction = newHeading.magneticHeading;
        if (direction<0) {
            return;
        }
        if (direction > 180)
        {
            direction = degrees+direction;
            direction = 360 - direction;
        }
        else
        {
            direction = degrees+direction;
            direction = 0 - direction;
        }
        // Rotate the arrow image
        if (self.delegate && [self.delegate respondsToSelector:@selector(compassAngleChange:)]) {
            [self.delegate compassAngleChange:direction];
        }
}
-(double) bearingToLocation:(CLLocation *) destinationLocation {

    double lat1 = DegreesToRadians(destinationLocation.coordinate.latitude);
    double lon1 = DegreesToRadians(destinationLocation.coordinate.longitude);

    double lat2 = DegreesToRadians(self.latitudeOfTargetedPoint);
    double lon2 = DegreesToRadians(self.longitudeOfTargetedPoint);

    double dLon = lon2 - lon1;

    double y = sin(dLon) * cos(lat2);
    double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
    double radiansBearing = atan2(y, x);

    return RadiansToDegrees(radiansBearing);
}
4

0 回答 0