1

我必须根据当前位置将箭头指向特定位置。为此,我使用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?

4

1 回答 1

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.
}
于 2013-10-10T07:38:46.177 回答