ios - CLLocationManager坐标

标签 ios objective-c xcode core-location cllocationmanager

我一直致力于实现步行、骑车和驾车的路线跟踪 map 。

但是,正如您在下面的屏幕截图中看到的,即使我没有步行/骑自行车或开车到那个位置,我的坐标也会时不时地突然跳跃。已在图像上绘制圆圈以指出问题。我的问题是为什么坐标会突然跳跃?

enter image description here

这是我的实现快照:

- (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/

相关文章:

iphone - MacRuby 的优点/限制是什么?有人用它为 iPhone 编程吗?

ios - 嵌套的 JSON 数据将导致使用 NS 字典崩溃(在 Swift 中)

swift - Xcode 图像解释

ios - 使用 Instruments 模拟后台获取

ios - 当 UIButton 调用其选择器操作时获取 EXC_BAD_ACCESS

ios - UITableView reloadData() 不刷新显示的单元格

ios - 如何让单个UIStackView的行高动态调整满屏?

ios - 仅更改一个 collectionview 单元格的对象

iphone - 将下载的数据放入字典的最佳方法

ios - Obj-C UITable 未填充 MutableArray