1

我创建了一个应用程序,我想在其中显示指南针。但我的应用程序显示空白页..我还添加了 CoreLocation 框架..请帮我显示指南针。

.h 文件代码:

import "UIKit/UIKit.h"
import "CoreLocation/CoreLocation.h"

@interface Compass_VIew : UIViewController<CLLocationManagerDelegate> 
    {

IBOutlet UIImageView *arrow;

 IBOutlet UIImageView *arrowC;

 CLLocationManager *locManager;

}

@property (nonatomic, retain) CLLocationManager *locManager;

@property(readonly, nonatomic) BOOL headingAvailable;

@end

.m 文件代码

didUpdateHeading 方法代码:

NSLog(@"New magnetic heading: %f", newHeading.magneticHeading);
NSLog(@"New true heading: %f", newHeading.trueHeading);

viewDidLoad 方法代码:

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[self navigationController] setNavigationBarHidden:NO animated:NO];
locManager = [[[CLLocationManager alloc] init] autorelease];
locManager.desiredAccuracy= kCLLocationAccuracyBest;
locManager.delegate = self;
[self.locManager startUpdatingHeading];
4

1 回答 1

2

你需要为指南针的针提供变换

应该是这样的

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor clearColor];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation];
    [locationManager startUpdatingHeading];

    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D user = [location coordinate];


}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    CLLocationCoordinate2D here =  newLocation.coordinate;


}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {

    double trueheading = newHeading.magneticHeading;
    double northangle = (trueheading*M_PI/180);
    redPin.transform = CGAffineTransformMakeRotation(northangle); 
}

这里 redPin 是我的针视图,顺便别忘了导入 CoreLocation 框架

于 2012-04-10T06:45:45.173 回答