CLLocationManager坐标

我一直在努力实施步行,骑自行车和驾驶的路线图。

但是,正如你在下面的屏幕截图中所看到的那样,即使我没有走路/骑自行车或者驾驶那个位置,我的坐标也会不时跳跃。 这个圆圈已经画在图像上来指出这个问题。 我的问题是为什么所有的突然坐标跳转?

在这里输入图像说明

这是我的实现快照:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { CoordinateModel *coord = [[CoordinateModel alloc] init]; coord.latitude = newLocation.coordinate.latitude; coord.longitude = newLocation.coordinate.longitude; ActivityType currentActivityType = [DataManager sharedInstance].activityType; if (currentActivityType == 0) { // walking [appDelegate.walkingCoordinates addObject:coord]; } else if(currentActivityType == 1) { [appDelegate.bikingCoordinates addObject:coord]; } else if(currentActivityType == 2) { // driving [appDelegate.drivingCoordinates addObject:coord]; } self.coordinate = newLocation.coordinate; } 

我build议你不要使用委托方法locationManager:didUpdateToLocation:fromLocation:它已被弃用。

您应该使用locationManager:didUpdateLocations来代替。

关于你的问题,像你提到的“跳跃”的位置是由于GPS在一定的时间内无法确定你的位置的准确性。 如果您logging坐标以及包括室内在内的所有时间的准确性 ,您将会意识到,当您在室内时的准确性不佳,您可能会在连接到Wifi时看到准确度1414。 当您在室内时,GPS不能正常工作。 所以,你的代码必须足够聪明才能画出一条path,或者只有坐标足够好的时候才能将坐标发送到服务器。

下面的代码是我用来过滤掉坏坐标的一些条件。

 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ for(int i=0;i<locations.count;i++){ CLLocation * newLocation = [locations objectAtIndex:i]; CLLocationCoordinate2D theLocation = newLocation.coordinate; CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy; NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; if (locationAge > 30.0) continue; //Select only valid location and also location with good accuracy if(newLocation!=nil&&theAccuracy>0 &&theAccuracy<2000 &&(!(theLocation.latitude==0.0&&theLocation.longitude==0.0))){ self.myLastLocation = theLocation; self.myLastLocationAccuracy= theAccuracy; NSMutableDictionary * dict = [[NSMutableDictionary alloc]init]; [dict setObject:[NSNumber numberWithFloat:theLocation.latitude] forKey:@"latitude"]; [dict setObject:[NSNumber numberWithFloat:theLocation.longitude] forKey:@"longitude"]; [dict setObject:[NSNumber numberWithFloat:theAccuracy] forKey:@"theAccuracy"]; //Add the valid location with good accuracy into an array //Every 1 minute, I will select the best location based on accuracy and send to server [self.shareModel.myLocationArray addObject:dict]; } } } 

经过一段时间(例如:3分钟)之后,我将再次从self.shareModel.myLocationArray中select最佳坐标,然后在地图上绘制坐标并将坐标发送到服务器。

您可以从这里看到完整的解决scheme和示例项目: 背景定位服务不适用于iOS 7

如果我的答案足够好,不要忘记加注。 ;)

同样的问题仍然在代码中。

在这里输入图像说明

  -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ iNEAT_o_GamesAppDelegate *appDelegate = (iNEAT_o_GamesAppDelegate *)[[UIApplication sharedApplication] delegate]; CoordinateModel *coord = [[CoordinateModel alloc] init]; ActivityType currentActivityType = [DataManager sharedInstance].activityType; for(int i=0;i<locations.count;i++){ CLLocation * newLocation = [locations objectAtIndex:i]; CLLocationCoordinate2D theLocation = newLocation.coordinate; CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy; NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; if (locationAge > 30.0) continue; //Select only valid location and also location with good accuracy if(newLocation!=nil&&theAccuracy>0 &&theAccuracy<2000 &&(!(theLocation.latitude==0.0&&theLocation.longitude==0.0))){ coord.latitude = theLocation.latitude; coord.longitude = theLocation.longitude; if (currentActivityType == 0) { // walking [appDelegate.walkingCoordinates addObject:coord]; } else if(currentActivityType == 1) { [appDelegate.bikingCoordinates addObject:coord]; } else if(currentActivityType == 2) { // driving [appDelegate.drivingCoordinates addObject:coord]; } } } }