我一直致力于为步行、骑自行车和开车实现路线跟踪地图。
然而,正如你在下面的截图中所看到的,我的坐标不时地跳跃,即使我没有步行/骑自行车或驾驶那个位置。为了指出问题,在图像上画了一个圆圈。我的问题是为什么突然间坐标会跳跃?

以下是我的实现快照:
- (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;
}发布于 2014-05-23 03:52:27
我建议您不要再使用委托方法locationManager:didUpdateToLocation:fromLocation:,它已经被废弃了。
您应该使用locationManager:didUpdateLocations代替。
关于你的问题,像你提到的那样,“跳跃”的位置是因为GPS无法在一定时间内确定你的位置的准确性。如果您一直记录坐标和精度,包括当您是时,您就会意识到,当您在室内时,当您连接到Wifi时,可能会看到精度1414。当你在室内时,GPS不能很好地工作。因此,您的代码必须足够聪明,只有在只有坐标足够好时,才能绘制路径或将坐标发送到服务器。
下面的代码是我用来过滤坏坐标的一些标准。
-(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中选择最佳坐标,并将坐标发送到服务器。
您可以从这里看到完整的解决方案和示例项目:Background Location Services not working in iOS 7
如果我的答案足够好,别忘了投一票。;)
发布于 2014-05-23 04:09:58
代码中仍然存在同样的问题。

-(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];
}
}
}
}https://stackoverflow.com/questions/23820252
复制相似问题