我怎样才能使指南针的针头朝南?
Posted
技术标签:
【中文标题】我怎样才能使指南针的针头朝南?【英文标题】:how can i make the compass needle head to south? 【发布时间】:2013-02-26 06:52:25 【问题描述】:我刚刚使用以下代码创建了一个指南针应用程序来获取航向,它运行良好,注意指针随着用户航向旋转:
-(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;
但是当我使用返回值旋转针时,针不会动态旋转。
任何人都可以帮助我使针旋转并指向特定位置??? 请帮忙!!
【问题讨论】:
针有什么作用?它是否仍然指向用户的标题?您尝试让针旋转到返回的轴承的代码在哪里?你需要发布那个。 我知道指针仍然指向用户的标题!!通过使用:arrowImage.transform = CGAffineTransformMakeRotation(heading);当我想使用返回值旋转指针时,我使用方位法:bearingValue = [self bearing:locationManager.location.coordinate.latitude:locationManager.location.coordinate.longitude:21.422495:39.826201]; arrowImage.transform = CGAffineTransformMakeRotation(bearingValue);但它不是旋转的,我就是想不通:( 【参考方案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);
现在你的指针指向一个特定的位置。
【讨论】:
【参考方案2】:什么时候调用该方法:
`bearingValue = [self bearing:locationManager.location.coordinate.latitude :locationManager.location.coordinate.longitude :21.422495 :39.826201];`
需要在末尾加上
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
经常更新指针。
【讨论】:
以上是关于我怎样才能使指南针的针头朝南?的主要内容,如果未能解决你的问题,请参考以下文章