计算罗盘航向到一个特定的坐标,而不是向北航向、坐标、向北、而不是

2023-09-11 04:45:56 作者:僞裝旳代名詞

我不能让这种算法权利。我试图做一个指南针指向在某个位置,而不是仅仅指向,让我们说,北部。什么是错的。我花了很多时间试图弄清楚这一点,但我无法找到它。任何想法?

   - (无效)locationManager:(CLLocationManager *)经理didUpdateHeading:(CLHeading *)newHeading {

双distanceEast =(location.longitude大于0&安培;&安培; otherLocation.longitude℃的)? 180  -  location.longitude + otherLocation.longitude  -  -180:otherLocation.longitude  -  location.longitude;

    如果(distanceEast℃,){
        distanceEast + = 360;
    }

    双distanceWest =(location.longitude&℃,安培;&安培; otherLocation.longitude大于0)? -180  -  location.longitude  -  180  -  otherLocation.longitude:location.longitude  -  otherLocation.longitude;

    如果(distanceWest℃,){
        distanceWest + = 360;
    }

    浮动latitudinalDifference =(otherLocation.latitude  -  location.latitude);
    浮longitudinalDifference = FMIN(distanceEast,distanceWest);

    浮ARCTAN = ATAN(longitudinalDifference / latitudinalDifference);

        浮动oldRadian =(-manager.heading.trueHeading * M_PI /180.0f)+arcTan+M_PI;
        浮动newRadian =(-newHeading.trueHeading * M_PI /180.0f)+arcTan+M_PI;

        CABasicAnimation *动画;
        动画= [CABasicAnimation animationWithKeyPath:@transform.rotation];
        animation.fromValue = [NSNumber的numberWithFloat:oldRadian]。
        animation.toValue = [NSNumber的numberWithFloat:newRadian]。
        animation.duration = 0.5F;
        directionsArrow.layer.anchorPoint = CGPointMake(0.5,0.5);

        [directionsArrow.layer addAnimation:动画forKey:@rotationCompass];
        directionsArrow.transform = CGAffineTransformMakeRotation(newRadian);
}
 

解决方案

 双LON = location.longitude  -  otherLocation.longitude;
双Y = SIN(LON)* COS(otherLocation.latitude);
双X = COS(location.latitude)*罪(otherLocation.latitude) - 罪(location.latitude)* COS(otherLocation.latitude)* COS(LON);
双角= ATAN2(Y,X);
 

角度是两个位置之间的方位。

八字算财运事业 五行与八字是什么 我们算命算什么

I can't get this algorithm right. I'm trying to make a compass that points at a certain location instead of just pointing to, let's say, north. Something is wrong. I've spent a lot of time trying to figure this out, but i just can't find it. Any ideas?

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{

double distanceEast = (location.longitude > 0 && otherLocation.longitude < 0) ? 180 - location.longitude + otherLocation.longitude - -180: otherLocation.longitude - location.longitude;

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

    double distanceWest = (location.longitude < 0 && otherLocation.longitude > 0) ? -180 - location.longitude - 180 - otherLocation.longitude : location.longitude - otherLocation.longitude;

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

    float latitudinalDifference = (otherLocation.latitude - location.latitude);
    float longitudinalDifference = fmin(distanceEast,distanceWest);

    float arcTan = atan(longitudinalDifference / latitudinalDifference);

        float oldRadian = (-manager.heading.trueHeading *M_PI /180.0f)+arcTan+M_PI;
        float newRadian = (-newHeading.trueHeading *M_PI /180.0f)+arcTan+M_PI;

        CABasicAnimation *animation;
        animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        animation.fromValue = [NSNumber numberWithFloat:oldRadian];
        animation.toValue = [NSNumber numberWithFloat:newRadian];
        animation.duration = 0.5f;
        directionsArrow.layer.anchorPoint = CGPointMake(0.5, 0.5);

        [directionsArrow.layer addAnimation:animation forKey:@"rotationCompass"];
        directionsArrow.transform = CGAffineTransformMakeRotation(newRadian);
}

解决方案

double lon = location.longitude - otherLocation.longitude;
double y = sin(lon) * cos(otherLocation.latitude);
double x = cos(location.latitude) * sin(otherLocation.latitude) - sin(location.latitude) * cos(otherLocation.latitude) * cos(lon);
double angle = atan2(y, x);

angle is the bearing between the two locations.