1

我正在使用以下命令发起本地化请求Grand Central Dispatch

- (void) findGroceriesNearMe {
    dispatch_queue_t downloadQueue = dispatch_queue_create("Groceries downloader", NULL);
    dispatch_async(downloadQueue, ^{
        CLLocationCoordinate2D userLocation = [LocationManagerController findMeWithCaller:self];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self userSuccessFullyFound:userLocation];
        });
    });
    dispatch_release(downloadQueue);
}

它在我的 Singleton 类 LocationManager Controller 中调用一个静态方法:

+ (CLLocationCoordinate2D) findMeWithCaller: (UIViewController *) viewController {

    LocationManagerController *locationManagerController = [LocationManagerController locationManagerController];
    [locationManagerController startUpdates];

    while(![locationManagerController getterDone]){
        //mystique pour nous-- a approfondir
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }

在 startUpdates 方法中, 的CLLocationManager, 属性LocationManagerController被初始化并要求startUpdatingLocation.

最后,位置更新发生时的方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation  fromLocation:(CLLocation *)oldLocation
{
    locationDenied = NO;
    NSLog(@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);

    NSDate* eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    // On vérifie que la newLocation est récente 

    if (abs(howRecent) > 10.0) {
        return;
    }

    // Test if it's not an invalid measurement

    if (newLocation.horizontalAccuracy < 0) return;

    // Test the measurement to see if it meets the desired accuracy 

    if (newLocation.horizontalAccuracy <= manager.desiredAccuracy) 

    {
        latitude = newLocation.coordinate.latitude;
        longitude = newLocation.coordinate.longitude;
        locationDefined = YES;
        [self setterDone:YES];
    }
}

我的问题是 locationManager 只发送 3 个位置更新然后停止发送更新,即使我没有要求它停止。所以基本上,我永远不会退出 while(![locationManagerController getterDone]) 循环。

顺便说一句,在尝试使用 GCD 实现它之前,它工作正常,所以我猜这个问题与我的多线程实现有关。

任何想法 ?

编辑

我在控制台中没有收到任何错误。该程序一直在运行,但我被困在那个 while 循环中,并且在 3 个第一个位置更新后没有其他任何事情发生。

谢谢 !

4

2 回答 2

3

从 CLLocationManager 类参考:

位置管理器对象的配置必须始终发生在具有活动运行循环的线程上,例如应用程序的主线程。

于 2011-12-20T14:33:43.670 回答
0

一个推测。如果您坐在办公桌前并使用模拟器进行测试,则准确性可能无法达到您想要的水平

if (newLocation.horizontalAccuracy <= manager.desiredAccuracy) 

因此,您可能会陷入循环。在办公桌前尝试更高的准确度。还要考虑精度是否永远不会比您想要的更好,因为它可能是 gps 接收不好。

让我知道这是否有帮助或者我是否偏离了标准:-)

-- 快触

于 2011-01-09T20:02:19.143 回答