【问题标题】:Delay in Corelocation核心定位延迟
【发布时间】:2015-06-22 20:42:06
【问题描述】:

我已经为我的位置需求创建了一个辅助类,因此我不会违反 DRY 原则。该类如下所示:
位置.h

@interface Location : NSObject <CLLocationManagerDelegate>{
CLLocationManager *manager;
CLGeocoder *geocoder;
CLPlacemark *placemark;
}

-(float)latitude;
-(float)longitude;
-(NSString *)postalcode;

位置.m

@implementation Location{
    float latitude;
    float longitude;
    NSString *postalcode;
}
-(id)init{
    NSLog(@"Hallo");
    [self setupLocationManager];
    return self;
}

-(float)latitude{

    return latitude;
}

-(float)longitude{

    return longitude;
}

-(NSString *)postalcode{

    return postalcode;
}

-(void)setupLocationManager{
    manager = [[CLLocationManager alloc] init];
    [manager requestWhenInUseAuthorization];
    manager.delegate = self;
    manager.desiredAccuracy = kCLLocationAccuracyBest;
    manager.distanceFilter = 100;
    [manager startUpdatingLocation];
    geocoder = [[CLGeocoder alloc] init];
 }

 #pragma mark - CLLocationManagerDelegate Methods

 - (void)locationManager:(CLLocationManager *)manager didFailWithError:          (NSError *)error
{

     NSLog(@"Error: %@", error);
     NSLog(@"Failed to get location! :(");

}

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{

NSLog(@"Location: %@", newLocation);
CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {

    latitude = currentLocation.coordinate.latitude;
    longitude = currentLocation.coordinate.longitude;


}

[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {

    if (error == nil && [placemarks count] > 0) {

        placemark = [placemarks lastObject];
        postalcode = [NSString stringWithFormat:@"%@",placemark.postalCode];
        /*
         self.address.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
         placemark.subThoroughfare, placemark.thoroughfare,
         placemark.postalCode, placemark.locality,
         placemark.administrativeArea,
         placemark.country];
         */
    } else {

        NSLog(@"%@", error.debugDescription);

    }

} ];

}


 @end

当我在我的 ViewController 中尝试创建 Location 实例并设置纬度和经度标签时,在 viewDidLoad 方法中,标签被设置为 0.00000。 显然,Location 需要大约半秒才能获得坐标。 我试过使用

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [self setCoordinateLabels];
    });

但这看起来很老套,不可能是最好的做法?那么有什么方法可以让我做得更好吗?

【问题讨论】:

    标签: ios objective-c location cllocationmanager


    【解决方案1】:

    嗯 - 这很 hacky。你为什么不转发你的委托方法调用?

    locationManager:didUpdateToLocation:
    

    (顺便说一句,这是一个遗留功能)

    在设置第一个位置时告诉您。您可以在 Location 类上设置一组委托,并在需要时调用每个委托。

    这是一个带有块的示例:

    static NSMapTable *listenerBlocks;
    
    + (void)addListener:(NSObject *)listener listenerBlock:(void (^)())listenerBlock
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            listenerBlocks =
            [NSMapTable mapTableWithKeyOptions:NSMapTableWeakMemory
                                  valueOptions:NSMapTableStrongMemory];
        });
        if (listenerBlock && listener) {
            [listenerBlocks setObject:listenerBlock forKey:listener];
        }
    }
    
    + (void)removeListener:(NSObject *)listener
    {
        if (listener) {
            [listenerBlocks removeObjectForKey:listener];
        }
    }
    

    在您的locationManager:didUpdateToLocation: 中,您只需拨打电话

    NSArray *allBlocks = [[listenerBlocks objectEnumerator] allObjects];
    for(void (^listenerBlock)(NSString *) in allBlocks)
    {
        listenerBlock();
    }
    

    最后

    在需要更新标签的类中(例如myLabel):

    [Location addListener:self listenerBlock:^{
       dispatch_async(dispatch_get_main_queue(),^{
          //myLabel.text = //... update your label here
          [self setCoordinateLabels]; // as from your code above..
       });
    }];
    

    【讨论】:

    • 感谢您的快速响应! :) 我从未见过任何关于 NSMapTable 的内容。所以第一个第一个代码示例(静态 NSMapTable *listenerBlocks)进入 location.m 文件然后我 didUpdateToLocation(仍然在 location.m 中,对吗?)得到代码的第二部分。我用''eventName''调用什么样的方法?我在那里设置标签吗?还是什么?
    • 忘记 eventName - 我在没有它的情况下更新代码示例,你不需要它。复制我编辑的代码!我还添加了一个使用示例,您可以在其中更新标签
    • 您需要 NSMapTable 来获得比 NSDictionary 更好的内存管理 - 但基本相同。听众不会被保留!
    猜你喜欢
    • 2016-08-12
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多