我正在尝试创建一个将获取位置和地址的NSObject,我还将运行其他一些代码。我想把它扔进我的不同应用程序。我有更新位置的问题。我有协议和委托工作,但我会发布我的所有代码,试图说清楚。
在我的getLocation.h
中@protocol getLocationDelegate
- (void)getLocation:(NSString *)address getLongitude:(CGFloat)longitude getLatitude:(CGFloat)latitude;
@end
@interface getLocation : NSObject <CLLocationManagerDelegate> {
CGFloat latitude;
CGFloat longitude;
NSString *address;
}
@property (nonatomic, strong) id<getLocationDelegate> delegate;
@property (nonatomic, retain) CLLocationManager *locationManager;
- (void)getLocationInfo;
在我的getLocation.m
中- (id)init
{
if (self = [super init]) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
return self;
}
- (void)getLocationInfo {
[self.locationManager startUpdatingLocation];
// This is being called but not starting the locationManager
}
在我的getLocation.h
中获取地址和我调用协议的位置之后[self.delegate getLocation:address getLongitude:longitude getLatitude:latitude];
在我的.m文件中,我想获取位置,这是代码
getLocation *location = [[getLocation alloc] init];
[location setDelegate:self];
[location getLocationInfo];
然后我在调用委托时使用我的方法
-(void)getLocation:(NSString *)address getLongitude:(CGFloat)longitude getLatitude:(CGFloat)latitude {
// Do code after location and address has been returned
}
现在我的协议和委托正在解决我的问题,我无法让locationManager开始更新。不知道最近发生了什么。
答案 0 :(得分:0)
我明白了,ARC在CoreLocation启动之前发布了getLocation *location = [[getLocation alloc] init];
。所以我只需要在我的.h中声明它并让它变得强大。