CLLocationManager坐标 [英] CLLocationManager Coordinates

查看:154
本文介绍了CLLocationManager坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力实现步行,骑自行车和驾车的路线跟踪图。

I have been working to implement route trace map for walking,biking and driving.

但是,如下面的屏幕截图所示,我的坐标跳了即使我没有走路/骑车或开车去那个地方,有时还是会突然发生。图像上已画出圆圈以指出问题所在。我的问题是为什么所有坐标突然跳动?

However, as you see in the following screenshot, my coordinate jumps all of a sudden from time to time even though I did not walk/bike or drive that location. The circle has been drawn on the image to point out the problem. My question is why all of a sudden coordinates jumps ?

这是我的实现快照:

- (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:不再适用。

I suggest you not to use the delegate method locationManager:didUpdateToLocation:fromLocation: anymore and it has been deprecated.

您应该改用 locationManager:didUpdateLocations

关于您的问题,您提到的位置跳跃是由于GPS在特定时间内无法确定您的位置的准确性。如果您一直记录坐标准确性(包括在室内 时),您将意识到当您处于呆在室内不好,当您连接到Wifi时可能会看到1414的精度。当您在室内时,GPS无法正常工作。因此,您的代码必须足够聪明,才能仅在坐标足够好时才绘制路径或将坐标发送到服务器。

About your question, the location "jumping" like you mention is due to the GPS that is unable to determine the accuracy of your location during a certain time. If you record down the coordinate and also the accuracy for all the time including when you are indoor, you will realize that the accuracy when you are staying indoor is not good, you might see the accuracy 1414 when you are connected to Wifi. GPS does not work well when you are indoor. So, your code has to be smart enough to only draw a path or send the coordinate to the server when only the coordinate is good enough.

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

The below code are some of the criteria that I use to filter out the bad coordinates.

-(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 中选择最佳坐标,然后在地图上绘制坐标并将其发送到服务器。

After a certain period (eg: 3 minutes), I will again choose the best coordinate from self.shareModel.myLocationArray before drawing the coordinate on map and send the coordinate to the server.

您可能会在这里看到完整的解决方案和示例项目:后台定位服务无法在iOS 7中运行

You may see the full solution and sample project from here: Background Location Services not working in iOS 7

如果我答案足够好。 ;)

Don't forget to upvote if my answer is good enough. ;)

这篇关于CLLocationManager坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆