2 在 viewdidLoad
中初始化locationManager
_locationManager = [[CLLocationManager alloc]init]; _locationManager.delegate = self; _locationManager.desiredAccuracy = kCLLocationAccuracyBest; [_locationManager requestAlwaysAuthorization]; [_locationManager startUpdatingLocation]; [_locationManager location];
3 实现代理方法 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
if (_startingLocation == nil) { _startingLocation = (CLLocation *)[locations firstObject]; } CLLocation *currentLocation = (CLLocation *)[locations lastObject]; _speed = currentLocation.speed; _didtance = [currentLocation distanceFromLocation:_startingLocation ];
4 以上即可利用GPS获取行车速度和距离