gpt4 book ai didi

ios - 核心定位延迟

转载 作者:行者123 更新时间:2023-11-29 12:19:40 24 4
gpt4 key购买 nike

我已经为我的位置需求创建了一个辅助类,所以我没有违反 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];
});

但这似乎很老套,不可能是最佳实践?那么我有什么更好的方法可以做到这一点吗?

最佳答案

好吧 - 这非常 hacky。为什么不转发委托(delegate)方法调用?

locationManager:didUpdateToLocation:

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

在设置第一个位置时通知您。您可以在 Location 类中只包含一组委托(delegate),并在适当的时候调用每个委托(delegate)。

这是一个 block 的例子:

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..
});
}];

关于ios - 核心定位延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30989449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com