利用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获取行车速度和距离


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM