尝试在您的视图控制器中输入此代码,它对我有用。
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = [[UIApplication sharedApplication] delegate];
locationManager =[[CLLocationManager alloc] init];
// Do any additional setup after loading the view, typically from a nib.
[userMap setCenterCoordinate: userMap.userLocation.coordinate animated: YES];
[self performSelector:@selector(checkForLogin) withObject:nil afterDelay:1];
[self startLocationServices];
// create LM
self.locationManager = [CLLocationManager new];
// set its delegate
[self.locationManager setDelegate:self];
// set configuration
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
// start location services
[self.locationManager startUpdatingLocation];
// create an oldLocation variable if one doesn't exist
}
- (void)startLocationServices
{
// create the Location Manager
if (self.locationManager == nil) {
self.locationManager = [CLLocationManager new];
}
// stop services
[self.locationManager stopUpdatingLocation];
[self.locationManager setDelegate:nil];
self.uilabel.text = @"0.0f";
}
// locationManager didUpdateToLocation FromLocation
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"%@", newLocation);
self.uilabel.text = [NSString stringWithFormat:@"%d %.f ", x, [newLocation speed] * 2.23694];
}
+ (NSString *) speedToMPH: (float) value
{
NSString *x = @"0.0 ";
if (value>0) {
float mile = 1609.344f;
float mph = value / mile * 3600;
x = [NSString stringWithFormat: @"%.f ", mph];
}
return x;
}
并将其添加到您的.h 文件中
@property (strong, nonatomic) IBOutlet UILabel *uilabel;
@property(nonatomic) int x;
@property (nonatomic,retain) CLLocationManager *locationManager;
+ (NSString *) speedToMPH: (float) value;