Sunday, April 17, 2016

locationManager: didEnterRegion: triggered without location change

Leave a Comment

I have an app that monitors some regions (geofencing) that each region represent a store, the app notifies the user whenever he approaches to a store.

For some reason, the app sends a notification every around 20 minutes when the user is already inside the region's circle.

It's all working fine, but when the user is inside a region for a long time the app will keep notifying him until he'll exit the region.

Any idea why is it happening? Thank you!

2 Answers

Answers 1

When you create a locationManager and call startUpdatingLocation, it starts giving you co-ordinates of your simulator or device continuously, with a very slight differences in the co-ordinate's values. You need to call stopUpdatingLocation.

 For your case you need to stopUpdatingLocation when user exist the region or you can record the last location like this:   -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations  {   CLLocation *newLocation = [locations lastObject]; [locationManager stopUpdatingLocation];   } 

Answers 2

We have the same thing happening. We just keep track of the region coordinate and when the new one comes in we have a method to determine if the new coordinate is 'significantly different' from the region we are in. If it is not, then we ignore it. If it is, then we take our next appropriate action. I hope that helps!

* UPDATE *

Here is how we determine that there was enough change to take action:

- (BOOL) radiiAreSignificantlyDifferent:(CLLocationDistance) newRadius oldRadius:(CLLocationDistance)oldRadius {     // radii are in kilometers     return (newRadius > oldRadius + 1.5 || newRadius < oldRadius - 1.5) ? YES : NO; }  - (BOOL) locationsAreSignificantlyDifferent:(CLLocation*) newLocation oldLocation:(CLLocation*)oldLocation {     BOOL different = NO;      if (oldLocation == nil) {         different = YES;     }else{         // have we moved at least a quarter mile?         different = ([newLocation distanceFromLocation:oldLocation] > 400.0)? YES : NO;     }      return different; } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment