【问题标题】:CLLocationManager CoordinatesCLLocationManager 坐标
【发布时间】:2014-07-12 06:48:13
【问题描述】:

我一直在努力为步行、骑自行车和驾车实施路线追踪地图。

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

这是我的实现快照:

- (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;
}

【问题讨论】:

  • 这里有问题吗?我没有看到问号。
  • 对不起,迈克尔。我添加了问题。我的问题是为什么突然坐标跳跃?

标签: ios objective-c xcode core-location cllocationmanager


【解决方案1】:

我建议您不要再使用委托方法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

如果我的回答足够好,别忘了点赞。 ;)

【讨论】:

  • 您好 Ricky,非常感谢您的详细解释,我刚刚按照您描述的方式实现了我的代码。请在下面查看我的代码。你认为这是你建议的方式吗?顺便说一句,它只被调用一次。并且位置数组只包含一个对象。
  • 没问题。我不知道您的应用程序的目的/行为/期望/要求和其他细节,所以,真的很难提出任何建议。它被调用一次,因为你没有移动。当你移动时,你会得到更多的坐标。可能您想将所有坐标放入一个数组中(例如:每 5 分钟一次),然后只选择最佳坐标。之后记得清空数组。
  • 您好 Ricky,再次非常感谢。我已将您的答案标记为正确的解决方案。您能否快速查看我在下面发布的代码。这是正确的吗?
  • 我觉得它看起来不错。但是,我仍然认为您应该将坐标放入数组中,然后在一段时间后使用另一种方法绘制地图。那会更准确。
  • 将坐标放入数组后,例如。 appDelegate.walkingCoordinates 需要再次过滤。可能你只选择精度低于 50 的放到地图上。
【解决方案2】:

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

  -(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];
                    }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多