i want to write a background app. In this app i have the currentUserLocation a location, which was set by the user. So what i want is to calculate the distance between this two locations and if the user arrive his destination or is in a specified range , start an event, like a local notification.
what i have
a ViewController Class (provides the MapView)
a MapView Class (provides the Map)
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.map = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.map.delegate = self;
[self addSubview:self.map];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.pausesLocationUpdatesAutomatically = NO;
self.locationManager.desiredAccuracy = [[[Manager getInstance] returnValueForKey:@"GPS"] intValue];
}
Then in locationManager: didUpdateLocations: Method
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *loc = [locations lastObject];
// calculate the distance between two points
float d = acos(sin(self.mapPin.coordinate.latitude/ 180*M_PI) *sin(self.map.userLocation.coordinate.latitude/ 180*M_PI)
+ cos(self.mapPin.coordinate.latitude/ 180*M_PI) *cos(self.map.userLocation.coordinate.latitude/ 180*M_PI) *cos(self.map.userLocation.coordinate.longitude/180*M_PI - self.mapPin.coordinate.longitude/ 180*M_PI));
d = d * 6378.137;
// calculate the distance between two points also
CLLocationDistance dist = [self.map.userLocation.location distanceFromLocation:loc];
if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive ){
[self calculateDistanz:loc.coordinate];
}else{
NSLog(@"dist = %f",dist/1000);
NSLog(@"d = %f",d);
if (d <= self.range ) {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
self.localNotif = [[UILocalNotification alloc] init];
if (self.localNotif == nil)
return;
self.localNotif.timeZone = [NSTimeZone defaultTimeZone];
self.localNotif.alertBody = @"Text";
self.localNotif.alertAction = @"Stop";
self.localNotif.soundName = @"Alarm.mp3";
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
self.localNotif.userInfo = infoDict;
self.localNotif.hasAction = YES;
[[UIApplication sharedApplication] presentLocalNotificationNow:self.localNotif];
}
}
}
I also register the app in info.plist as background app for local updates. So problem is that the calculation stops when the entering in background mode. the method -(void) locationManager: didUpdateLocations: is called because the log statements are shown in the console. and there i can see that in this moment, when the app entering in background mode the variable d is always the same. until the app is active, then d is updating.
I have read and read for one week, but i can't find the mistake... maybe you can help thanks for help...