CLLocationManager 坐标

2024-04-14

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

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

这是我的实施快照:

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

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

你应该使用位置管理器:didUpdateLocations反而。

关于你的问题,像你提到的位置“跳跃”是由于GPS无法确定你在某个时间段内的位置的准确性。如果您记录下协调还有accuracy一直以来,包括当你在的时候indoor,你会发现,当你呆在室内时,精度不太好,当你连接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在地图上绘制坐标并将坐标发送到服务器之前。

您可以从这里看到完整的解决方案和示例项目:后台定位服务在 iOS 7 中不工作 https://stackoverflow.com/questions/18946881/background-location-services-not-working-in-ios-7/21966662#21966662

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CLLocationManager 坐标 的相关文章

随机推荐