首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CLLocationManager坐标

CLLocationManager坐标
EN

Stack Overflow用户
提问于 2014-05-23 02:54:37
回答 2查看 1.4K关注 0票数 3

我一直致力于为步行、骑自行车和开车实现路线跟踪地图。

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

以下是我的实现快照:

代码语言:javascript
复制
- (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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-23 03:52:27

我建议您不要再使用委托方法locationManager:didUpdateToLocation:fromLocation:,它已经被废弃了。

您应该使用locationManager:didUpdateLocations代替。

关于你的问题,像你提到的那样,“跳跃”的位置是因为GPS无法在一定时间内确定你的位置的准确性。如果您一直记录坐标精度,包括当您是时,您就会意识到,当您在室内时,当您连接到Wifi时,可能会看到精度1414。当你在室内时,GPS不能很好地工作。因此,您的代码必须足够聪明,只有在只有坐标足够好时,才能绘制路径或将坐标发送到服务器。

下面的代码是我用来过滤坏坐标的一些标准。

代码语言:javascript
复制
-(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

如果我的答案足够好,别忘了投一票。;)

票数 5
EN

Stack Overflow用户

发布于 2014-05-23 04:09:58

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

代码语言:javascript
复制
  -(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];
                    }
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23820252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档