我正在构建一个应用程序,当达到最大速度限制“120 公里/小时”时通知用户
但是在驾驶时测试应用程序时我没有得到准确的速度值
假设我以 80 公里/小时的速度行驶,我从应用程序获得的值介于 60 到 100 之间,有时每秒变化更多。
这是我正在使用的整个代码
视图控制器.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "CoreLocationController.h"
#import <MapKit/MapKit.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate, CoreLocationControllerDelegate> {
MKMapView *mapview;
CLLocationManager *locationManager;
IBOutlet UILabel *speedLabel;
CoreLocationController *CLController;
int distance;
int currentSpeed;
int maxSpeed;
IBOutlet UILabel *distanceLbl;
IBOutlet UILabel *maxSpeedLbl;
IBOutlet UILabel *currentSpeedLbl;
}
- (IBAction)Getlocation:(id)sender;
@property (nonatomic, retain) IBOutlet UILabel *distanceLbl;
@property (nonatomic, retain) IBOutlet UILabel *maxSpeedLbl;
@property (nonatomic, retain) IBOutlet UILabel *currentSpeedLbl;
@property (retain, nonatomic) IBOutlet UILabel *speedLabel;
@property (nonatomic, retain) CoreLocationController *CLController;
@property (retain, nonatomic) IBOutlet MKMapView *mapview;
@property (strong, nonatomic) IBOutlet CLLocationManager *locationManager;
视图控制器.m
@synthesize distanceLbl,maxSpeedLbl,currentSpeedLbl;
@synthesize locationManager;
@synthesize mapview;
@synthesize speedLabel;
@synthesize CLController;
- (void)locationChange:(CLLocation *)newLocation:(CLLocation *)oldLocation {
NSTimeInterval difference = [[newLocation timestamp] timeIntervalSinceDate:[oldLocation timestamp]];
double temp_distance = [newLocation getDistanceFrom:oldLocation];
distance += temp_distance;
distanceLbl.text = [NSString stringWithFormat:@"%.2d",distance];
currentSpeed = (temp_distance/difference) * (18.0/5.0);
if (currentSpeed > maxSpeed) {
maxSpeed = currentSpeed;
maxSpeedLbl.text = [NSString stringWithFormat:@"%.2d",maxSpeed];
}
currentSpeedLbl.text = [NSString stringWithFormat:@"%.2d",currentSpeed];
}
- (void)locationUpdate:(CLLocation *)location {
speedLabel.text = [NSString stringWithFormat:@"SPEED: %f", [location speed]];
self.mapview.showsUserLocation = YES;
}
- (void)locationError:(NSError *)error {
speedLabel.text = [error description];
}
- (IBAction)Getlocation:(id)sender {
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
[mapview setMapType:MKMapTypeStandard];
[mapview setZoomEnabled:YES];
[mapview setScrollEnabled:YES];
self.mapview.showsUserLocation = YES;
MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
region.center.latitude = locationManager.location.coordinate.latitude;
region.center.longitude = locationManager.location.coordinate.longitude;
region.span.longitudeDelta = 0.007f;
region.span.latitudeDelta = 0.007f;
[mapview setRegion:region animated:YES];
[mapview setDelegate:sender];
}
- (IBAction)setMap:(id)sender {
switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
case 0:
mapview.mapType = MKMapTypeStandard;
break;
case 1:
mapview.mapType = MKMapTypeSatellite;
break;
case 2:
mapview.mapType = MKMapTypeHybrid;
break;
default:
break;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
CLController = [[CoreLocationController alloc] init];
CLController.delegate = self;
[CLController.locMgr startUpdatingLocation];
[self Getlocation:self];
maxSpeed = 120;
}
核心位置控制器.h
@protocol CoreLocationControllerDelegate
@required
- (void)locationUpdate:(CLLocation *)location;
- (void)locationError:(NSError *)error;
- (void)locationChange:(CLLocation *)newLocation:(CLLocation *)oldLocation;
@end
@interface CoreLocationController : NSObject <CLLocationManagerDelegate> {
CLLocationManager *locMgr;
id delegate;
//new speed method
int distance;
int currentSpeed;
int maxSpeed;
IBOutlet UILabel *distanceLbl;
IBOutlet UILabel *maxSpeedLbl;
IBOutlet UILabel *currentSpeedLbl;
}
@property (nonatomic, retain) IBOutlet UILabel *distanceLbl;
@property (nonatomic, retain) IBOutlet UILabel *maxSpeedLbl;
@property (nonatomic, retain) IBOutlet UILabel *currentSpeedLbl;
@property (nonatomic, retain) CLLocationManager *locMgr;
@property (nonatomic, assign) id delegate;
核心位置控制器.m
@synthesize locMgr;
@synthesize delegate = _delegate;
@synthesize distanceLbl,maxSpeedLbl,currentSpeedLbl;
- (id)init {
self = [super init];
if(self != nil) {
self.locMgr = [[CLLocationManager alloc] init];
self.locMgr.delegate = self;
}
return self;
}
- (void)locationChange:(CLLocation *)newLocation:(CLLocation *)oldLocation {
NSTimeInterval difference = [[newLocation timestamp] timeIntervalSinceDate:[oldLocation timestamp]];
double temp_distance = [newLocation getDistanceFrom:oldLocation];
distance += temp_distance;
distanceLbl.text = [NSString stringWithFormat:@"%.2d",distance];
currentSpeed = (temp_distance/difference) * (18.0/5.0);
if (currentSpeed > maxSpeed) {
maxSpeed = currentSpeed;
maxSpeedLbl.text = [NSString stringWithFormat:@"%.2d",maxSpeed];
}
currentSpeedLbl.text = [NSString stringWithFormat:@"%.2d",currentSpeed];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) {
[self.delegate locationUpdate:newLocation];
[self.delegate locationChange:newLocation :oldLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) {
[self.delegate locationError:error];
}
}