After reading some recommendations on stack overflow and some other articles I have implemented a singleton class that allows me to call a method to update a user's current location. To avoid continuous update of the location information, I added a StopUpdatingPosition call. This works fine for the first time, however following calls fail.
I believe this is because of the way the geolocation stuff is initiated in the examples provided where it checks to see if the singleton is already in place(?)
If a singleton is initialised does it remain in memory? On subsequent calls can I check to see if this is the case and just request it to start updating position?
Here is what I have tried so far.
- (id)init {
self = [super init];
if(self) {
self.locationManager = [CLLocationManager new];
[self.locationManager setDelegate:self];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
[self.locationManager setHeadingFilter:kCLHeadingFilterNone];
[self.locationManager startUpdatingLocation];
geocoder = [[CLGeocoder alloc] init];
}
return self;
}
+ (LocationFunction*)sharedSingleton {
static LocationFunction* sharedSingleton;
if(!sharedSingleton) {
@synchronized(sharedSingleton) {
sharedSingleton = [LocationFunction new];
}
}
return sharedSingleton;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
myLat = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
myLong = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
}
// Store the Long & Lat to global variables here
NSLog(@"Resolving the Address");
} else {
// Handle location lookup fail here
}
} ];
[manager stopUpdatingLocation];
}
I am looking for a way of calling method in an external class from different view controllers. This method obtains the user's current position (Long & Lat), records the info in a global variable, stops any requests to update location method and notifies the view controller when it is done.
Pointers appreciated.