我刚刚使用以下代码创建了一个指南针应用程序来获取航向,它运行良好,请注意指针随着用户航向旋转:
-(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;
}
但是当我使用返回的值来旋转针头时,针头不会动态旋转。
任何人都可以帮助我使针旋转并指向特定位置???请帮忙!!