【问题标题】:CLLocationManager weird issuesCLLocationManager 奇怪的问题
【发布时间】:2011-09-05 19:58:29
【问题描述】:

我有一个 MKMapView,每次加载视图或调用 showLocation 自定义类方法时,我都会删除一个注释。

我需要准确度是最好的

-(void)viewDidLoad {
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    [locationManager startUpdatingLocation];
}
-(IBAction) showLocation:(id) sender{
    [locationManager startUpdatingLocation];
}
- (void) locationManager:(CLLocationManager *) manager
     didUpdateToLocation:(CLLocation *) newLocation
            fromLocation:(CLLocation *) oldLocation {
// start geocoding with newLocation coordinate which will automatically set annotation.
SVGeocoder *geocodeRequest = [[SVGeocoder alloc] 
                                      initWithCoordinate:CLLocationCoordinate2DMake(newLocation.coordinate.latitude, newLocation.coordinate.longitude)];
        [geocodeRequest setDelegate:self];
        [geocodeRequest startAsynchronous];
        [geocodeRequest release];
            [locationManager stopUpdatingLocation];
}

我的问题是什么时候会调用didUpdateToLocation 方法?只有在我执行[locationManager startUpdatingLocation]时找到新位置后?

当用户在旅行和静止不动时,我遇到了一些奇怪的问题。 假设用户从点 A->B->C->D 出发,点之间有 1 分钟的间隔。当我在 C 点调用我的方法时,它有时会返回 A 点的坐标,有时会返回 B 点,有时会返回 C。这只是随机的。

当我静止不动时,情况就更奇怪了。当我调用 showLocation 方法时,我得到不同的坐标,即使我连接到我家的 WiFi。

我正在考虑实施 didUpdateToLocation 以在 5 秒内获得最佳结果。如果在 5 秒内,它找到了我定义的精度的特定位置,则使用坐标。如果没有,请使用它在 5 秒时间范围内找到的最佳值。但由于我是新手,我不确定如何编写这样的代码。我阅读了 NSTimer,它似乎可以工作。

各位有什么建议吗?

提前非常感谢!

【问题讨论】:

    标签: iphone ios cllocationmanager


    【解决方案1】:

    您从 A 点接收位置的原因之一是 CoreLocation 正在返回它首先拥有的最后一个有效位置,直到它可以获得更准确的位置。当您调用[locationManager startUpdatingLocation]; 时,它会一遍又一遍地返回-didUpdateToLocation,直到您满意并最终调用-stopUpdatingLocation

    我认为在停止更新您的位置之前,您只需要留出一点时间来获得更好的位置修复。我会考虑将停止更新位置从您的-didUpdateToLocation 移动到其他方法。

    【讨论】:

      【解决方案2】:

      移除 [locationManager stopUpdatingLocation];从您的代码中尝试 .

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-01
        • 1970-01-01
        • 2015-02-27
        • 2018-06-26
        • 2011-08-13
        • 2011-08-17
        • 2011-02-02
        相关资源
        最近更新 更多