利用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