利用GPS获取行车速度和距离

这几天项目中需要GPS计算汽车的速度和行驶距离,这里简单记录一下使用过程

  • 1 和平常使用地图一样,在Info.plist中添加位置请求

  • 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获取行车速度和距离
原文地址:https://www.cnblogs.com/myqiqiang/p/4224268.html