我想实现的目标:我想在视图中显示用户地址。视图由视图控制器处理。此 VC 正在使用 GeoAPI 来访问用户位置。GeoAPI 包裹在 LocationManager-Class 周围,后者又负责处理所有 locationmanager 的东西。在这个位置管理器类中,我想如上所述反向地理编码。
geoAPI 有一些方便的方法可以用来获取我想要的信息。
地理API:
#import <Foundation/Foundation.h>
@interface MFGeoAPI : NSObject
@property (nonatomic, strong) NSDictionary *userLocationInfoDict;
+ (MFGeoAPI *)sharedInstance;
- (CLLocation *)getCurrentUserLocation;
@end
@interface MFGeoAPI () {
LocationManager *locationManager;
MotionManager *motionManager;
}
@end
@implementation MFGeoAPI
+(MFGeoAPI *)sharedInstance {
static MFGeoAPI *_sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedInstance = [[MFGeoAPI alloc] init];
});
return _sharedInstance;
}
- (id)init {
if (self = [super init]) {
locationManager = [[LocationManager alloc] initLocationManager];
motionManager = [[MotionManager alloc] initMotionManager];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getUserLocationInformation:) name:@"UserLocationInfo" object:nil];
return self;
}
- (CLLocation *)getCurrentUserLocation {
return [locationManager getCurrentUserLocation];
}
- (void)getUserLocationInformation:(NSNotification *)notification {
NSLog(@"OK");
self.userLocationInfoDict = [[NSDictionary alloc] initWithDictionary:notification.userInfo];
}
@end
现在..由于反向地理编码功能的异步字符的事实,我不确定何时可以使用带有用户地址的字典。
位置管理器类包含以下内容:
位置经理:
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface LocationManager : NSObject <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLLocation *userLocation;
@property (nonatomic, strong) NSDictionary *userLocDict;
- (id)initLocationManager;
- (CLLocation *)getCurrentUserLocation;
@end
#import "LocationManager.h"
@implementation LocationManager
- (id)initLocationManager {
if (self = [super init]) {
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
if (![self.locationManager locationServicesEnabled]) {
UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[servicesDisabledAlert show];
}
}
[self startUpdatingLocation];
[self startFetchingCurrentUserLocationInformation];
}
return self;
}
- (void)startUpdatingLocation {
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.headingFilter = kCLHeadingFilterNone;
[self.locationManager startUpdatingHeading];
[self.locationManager startUpdatingLocation];
self.userLocation = self.locationManager.location;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"FAILED");
}
- (CLLocation *)getCurrentUserLocation {
return self.userLocation;
}
- (void)startFetchingCurrentUserLocationInformation {
[self delayedReverseGeocodeLocation];
}
- (void)delayedReverseGeocodeLocation {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self reverseGeocodeLocation];
}
- (void)reverseGeocodeLocation {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:self.userLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSDictionary *dict = [[placemarks firstObject] addressDictionary];
if (placemarks.count > 0) {
NSDictionary *info = [NSDictionary dictionaryWithObject:dict forKey:@"transferDict"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"UserLocationInfo" object:self userInfo:info];
}
}];
}
在视图控制器中,我想访问我通过 API 传递的 userLocation 信息字典
视图控制器
- (id)init {
if (self = [super init]) {
self.userLocation = [[MFGeoAPI sharedInstance] getCurrentUserLocation];
NSLog(@"%@", [[MFGeoAPI sharedInstance].userLocationInfoDict description]);
}
return self;
}
但是 userlocationInfoDict 是空的。我想这是因为我的函数调用“startFetchingCurrentUserLocationInformation”在异步块返回我需要的值之前返回。然后我想通过 NSNotificationCenter 处理这个问题,我会在数据可用时收到通知。但现在我被困住了:
我需要做什么才能访问 GeoAPI 传递的 userLocationInformation 以便在数据到达时及时执行此操作?
任何帮助表示赞赏!谢谢塞巴斯蒂安