3 xcode objective-c core-location cllocationmanager ios
我一直在努力实现步行,骑自行车和驾驶的路线追踪图.
但是,正如您在下面的屏幕截图中看到的那样,即使我没有步行/骑自行车或驾驶该位置,我的坐标也会不时突然跳起.已在图像上绘制圆圈以指出问题.我的问题是为什么突然坐标跳跃?

这是我的实现快照:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
我建议你不要再使用委托方法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];
}
}
}
Run Code Online (Sandbox Code Playgroud)
经过一段时间(例如:3分钟)后,我将再次从self.shareModel.myLocationArray中选择最佳坐标,然后在地图上绘制坐标并将坐标发送到服务器.
您可以从此处看到完整的解决方案和示例项目:后台位置服务在iOS 7中不起作用
如果我的答案足够好,请不要忘记投票.;)
| 归档时间: |
|
| 查看次数: |
1223 次 |
| 最近记录: |