代码之家  ›  专栏  ›  技术社区  ›  Crystal

使用CLLocationManager获取准确位置

  •  3
  • Crystal  · 技术社区  · 15 年前

    我开始使用CLLocationManager,并尝试使用下面的回调方法。我出去用我的iPhone测试它,在最初的几次更新中,我似乎得到了一些乱七八糟的值,就像突然之间,我的距离是39米,尽管我没有移动任何地方。或者有时它会在5点开始,然后跳到20米,再也没有我移动的地方。我走出去走了,从最初的起点开始的更新看起来“还可以”,然后当我走回去的时候,我回到了最初的39米起点。我在想我下面所做的是否正确。我还包括了viewDidLoad方法,在这里我初始化了CLLocationManager对象。

    有没有办法确保我的第一个值是准确的?提前谢谢。

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.distanceFilter = kCLDistanceFilterNone;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    }
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        if (startingPoint == nil) {
            NSLog(@"NIL starting point");
            self.startingPoint = newLocation;
        }
    
        CLLocationDistance distance = [newLocation distanceFromLocation:startingPoint];
        NSString *distanceString = [[NSString alloc] initWithFormat:@"%g m", distance];
        distanceLabel.text = distanceString;
        [distanceString release];
    
        numberOfUpdates++;
        NSString *countString = [[NSString alloc] initWithFormat:@"%d", numberOfUpdates];
        countLabel.text = countString;
        [countString release];
    }
    
    3 回复  |  直到 12 年前
        1
  •  5
  •   Brad    15 年前

    GPS是一项不完善的技术。大气条件、卫星的可用性(和位置)、天空能见度、附近物体(建筑物)反射的信号都是造成其固有误差的原因。即使有一个“准确度”值 通常

    飞机不允许使用全球定位系统进行精确的接近——即使他们的接收器不够精确,他们还需要其他技术(它们有自己的问题)。

    尝试运行标准的“Maps”应用程序并将其用作比较。我觉得你的密码很好-是jut GPS。

    当然我这么说是因为我正在开发自己的航海应用程序,我自己也遇到了这些问题。

        2
  •  3
  •   hol    14 年前

    虽然这是个老问题,但我还是喜欢回答。对“didUpdateToLocation”的第一个调用通常是一些旧值,您应该始终检查时间戳。从Apple文档中:

    NSDate* eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 15.0)
    {
       // use the value
    }
    
        3
  •  2
  •   Dan Ray    15 年前

    didUpdate 方法,你可以测试 newLocation .horizontalAccuracy