1

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

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

}

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

任何人都可以帮助我使针旋转并指向特定位置???请帮忙!!

4

2 回答 2

1

存储返回的值

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

现在你的指针指向一个特定的位置。

于 2013-11-18T10:17:28.943 回答
0

您何时调用该方法:

`bearingValue = [self bearing:locationManager.location.coordinate.latitude :locationManager.location.coordinate.longitude :21.422495 :39.826201];`  

您需要在末尾添加它

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

经常更新针。

于 2013-02-26T08:27:53.323 回答