我必须根据当前位置将箭头指向特定位置。为此,我使用didUpdateHeading
委托方法获取真正的标题值。
在iPhone(人像模式)中,箭头图像显示完美。但在iPad中它显示不正确。它处于横向模式。
我用来查找轴承距离的代码是,
- (void)UpdateCompass:(NSNotification *)notification
{
CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:m_nLatitude longitude:m_nLongitude];
CLLocation* target = [[CLLocation alloc] initWithLatitude:questionLatitude longitude:questionLangitude ];
_latestBearing = [self getHeadingForDirectionFromCoordinate:newLocation.coordinate toCoordinate:target.coordinate];
CGFloat degrees = _latestBearing - m_CLHeading.magneticHeading;
CGAffineTransform cgaRotate = CGAffineTransformMakeRotation([self degreesToRadians:degrees]);
m_arrowImage.transform = cgaRotate;
}
- (double)degreesToRadians:(double)degrees
{
return degrees * M_PI / 180.0;
}
- (double)radiansToDegrees:(double)radians
{
return radians * 180.0/M_PI;
}
- (double)getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{
double lat1 = [self degreesToRadians:fromLoc.latitude];
double lon1 = [self degreesToRadians:fromLoc.longitude];
double lat2 = [self degreesToRadians:toLoc.latitude];
double lon2 = [self degreesToRadians:toLoc.longitude];
double dLon = lon2 - lon1;
double y = sin(dLon) * cos(lat2);
double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
double radiansBearing = atan2(y, x);
if(radiansBearing < 0.0)
radiansBearing += 2*M_PI;
return [self radiansToDegrees:radiansBearing];
}
如何让这项功能适用于iPad?
答案 0 :(得分:0)
检查设备是ipad还是iphone并相应地编写代码
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// The device is an iPad running iPhone 3.2 or later.
}
else
{
// The device is an iPhone or iPod touch.
}