我发现将 location 用作单例并将值保存到 default user 是很有趣的。我是年轻的程序员,正在尝试用 oop 编写所有代码。我使用它如下(此代码仍然需要重构,并且 alertUserWithTitle: 是 NYMessageToUser 的类方法来提醒用户):
//##Header file:
@interface NYLocationManager : NSObject<CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
float lonngitude;
float latitude;
float altitude;
}
@property(nonatomic,retain)CLLocationManager *locationManager;
@property(nonatomic,readwrite)float longitude;
@property(nonatomic,readwrite)float latitude;
@property(nonatomic,readwrite)float altitude;
+(NYLocationManager *) getInstance;
-(void)startUpdatingLocation;
-(void)stopUpdatingLocation;
-(double)getDistanceFromUserLocationToCordinatesLatitude:(float)lat Longitude:(float)lon;
@end
//### implementation file:
@implementation NYLocationManager
@synthesize locationManager;
@synthesize latitude;
@synthesize longitude;
@synthesize altitude;
+ (id)getInstance
{
static NYLocationManager *Instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Instance = [[self alloc] init];
});
[Instance startUpdatingLocation];
return Instance;
}
- (id)init
{
if (self = [super init])
{
latitude =0.0;
longitude =0.0;
altitude =0.0;
if([[NSUserDefaults standardUserDefaults] objectForKey:@"locationLongitude"] != nil)
{
NSUserDefaults *savedLocation=[NSUserDefaults standardUserDefaults];
latitude =[[savedLocation objectForKey:@"locationLatitude"] floatValue];
longitude =[[savedLocation objectForKey:@"locationLongitude"] floatValue];
altitude =[[savedLocation objectForKey:@"locationAltitude"] floatValue];
}
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
locationManager.delegate = self;
if ([CLLocationManager locationServicesEnabled])
{
[locationManager startUpdatingLocation];
} else
{
[NYMessageToUser alertUserWithTitle:@"Location Services is Disabled!!!" withMessage:@"This app is designed to share images with location, Please enable location for this app and relucnh the app"];
}
}
return self;
}
- (void)dealloc
{
// Should never be called, but just here for clarity really.
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *loc =[locations lastObject];
self.longitude =loc.coordinate.longitude;
self.latitude =loc.coordinate.latitude;
self.altitude =loc.altitude;
NSUserDefaults *savedLocation=[NSUserDefaults standardUserDefaults];
[savedLocation setObject: [NSString stringWithFormat:@"%f", self.longitude] forKey:@"locationLongitude"];
[savedLocation setObject: [NSString stringWithFormat:@"%f", self.latitude] forKey:@"locationLatitude"];
[savedLocation setObject: [NSString stringWithFormat:@"%f", self.altitude ] forKey:@"locationAltitude"];
[savedLocation synchronize];
[locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
[locationManager stopUpdatingLocation];
[NYMessageToUser alertUserWithTitle:@"Location Error!!!" withMessage:@"This app is designed to use with valid location, Please enable location for this app and relucnh the app"];
}
-(void)startUpdatingLocation
{
if ([CLLocationManager locationServicesEnabled])
{
[locationManager startUpdatingLocation];
} else
{
[NYMessageToUser alertUserWithTitle:@"Location Services is Disabled!!!" withMessage:@"This app is designed to share images with location, Please enable location for this app and relucnh the app"];
}
}
-(void)stopUpdatingLocation
{
[locationManager stopUpdatingLocation];
}
-(double)getDistanceFromUserLocationToCordinatesLatitude:(float)lat Longitude:(float)lon
{
CLLocation *locA = [[CLLocation alloc] initWithLatitude:self.latitude longitude:self.longitude];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
CLLocationDistance distance = [locA distanceFromLocation:locB];
return distance;
}
@end
//### How to use
NYLocationManager *loc =[NYLocationManager getInstance];
NSLog(@"longitude: %f, latitude: %f, altitude: %f",loc.longitude,loc.latitude,loc.altitude);