我刚刚使用以下代码创建了一个罗盘应用程序来获取航向,它运行良好,请注意针随着用户航向旋转:
-(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/