ios - 我怎样才能让罗盘针头朝南?

标签 ios objective-c xcode core-location cllocationmanager

我刚刚使用以下代码创建了一个罗盘应用程序来获取航向,它运行良好,请注意针随着用户航向旋转:

-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
    UIDevice *device = [UIDevice currentDevice];
    if (newHeading.headingAccuracy >0) {
        float magnaticHead = [self magneticHeading:newHeading.magneticHeading fromOrientation:device.orientation];
        float trueHead = [self trueHeading:newHeading.trueHeading fromOrientation:device.orientation];
         my= trueHead;

        magneticHeadingLabel.text = [NSString stringWithFormat:@"%f", magnaticHead];
        trueHeadingLabel.text = [NSString stringWithFormat:@"%f",trueHead];
        heading = -1.0f * M_PI * newHeading.magneticHeading / 180.0f;
      arrowImage.transform = CGAffineTransformMakeRotation(heading);

    }

所以我想让针指向一个特定的位置,该位置具有我当前位置的已知经度和纬度。因此我使用以下代码计算我与已知位置之间的方位:

-(CGFloat)bearing:(double)latitude1:(double)longitude1:(double)latitude2:(double)longitude2{

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

    float dx = 21.422495 - user.latitude;
    float dy = 39.826201 - user.longitude;
     angle = RADIANS_TO_DEGREES(atan2(dy, dx));


    // Set Latitude and Longitude

    latitude1 = DEGREES_TO_RADIANS(latitude1);
    latitude2 = DEGREES_TO_RADIANS(latitude2);
    // Calculate Difference

    double longitudeDifference = DEGREES_TO_RADIANS(longitude2 - longitude1);
    // Returns the Sine and the Cosine of the specified angle

    double y = sin(longitudeDifference) * cos(latitude2);
    double x = cos(latitude1) * sin(latitude2) - sin(latitude1)* cos(latitude2) * cos(longitudeDifference);
    // Return Bearing
    double rTD =(RADIANS_TO_DEGREES(atan2(y, x))+360) ;
     CGFloat bearingValue = (float)((int)rTD % (int)360);


     return  bearingValue;

}

但是当我使用返回值来旋转指针时,指针不会动态旋转。

谁能帮我让指针旋转并指向特定位置??? 请帮忙!!

最佳答案

存储从

返回的值
-(CGFloat)bearing:(double)latitude1:(double)longitude1:(double)latitude2:(double)longitude2;

在如下变量中:

degrees = [ self bearing:here.latitude :here.longitude :21.422495 :39.826201 ];

在方法之后:

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

获取头部并使针旋转:

needle.transform = CGAffineTransformMakeRotation((degrees-newHeading.trueHeading) * M_PI / 180);

现在您的指针指向特定位置。

关于ios - 我怎样才能让罗盘针头朝南?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15083101/

相关文章:

ios - 如何在关闭 View Controller 时获得淡出动画

ios - 如何使用 Swift 在 iOS 中设置透明背景图片?

ios - 当应用程序在后台运行时显示 UIAlert?

ios - 水平空间限制仅在 iPhone 6 Plus 中不起作用?

ios - iOS 7.1 SDK 中的内联日期选择器

javascript - 仅在 "body"元素上禁用拖动?

ios - iOS设计中 View 和容器 View 有什么区别?

ios - NSMutableArray 只有最后一个对象的副本

ios - Xcode 11 beta 无法在以证书问题结束的设备上安装应用程序

xcode - 如何本地化同名图像?