我一直致力于实现步行、骑车和驾车的路线跟踪 map 。
但是,正如您在下面的屏幕截图中看到的,即使我没有步行/骑自行车或开车到那个位置,我的坐标也会时不时地突然跳跃。已在图像上绘制圆圈以指出问题。我的问题是为什么坐标会突然跳跃?
这是我的实现快照:
- (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;
}
最佳答案
我建议您不要再使用委托(delegate)方法 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中选择最佳坐标,然后在 map 上绘制坐标并将坐标发送到服务器。
您可以从这里看到完整的解决方案和示例项目:Background Location Services not working in iOS 7
如果我的回答足够好,请不要忘记点赞。 ;)
关于ios - CLLocationManager坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23820252/