這幾天項目中需要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獲取行車速度和距離