Geolocation with local notification like reminder

2019-02-05 10:20发布

问题:

i want implement geolocation notification like the app reminders. this is what i have already done:

in App delegate:

self.locationManager = [[[CLLocationManager alloc] init] autorelease]; /* don't leak memeory! */
[self.locationManager setDelegate:self];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{

}

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{

}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{

}

And this one in the view controller witch starts monitoring:

CLLocationCoordinate2D location2D = mapView.region.center; 
CLRegion *regionForMonitoring = [[CLRegion alloc] initCircularRegionWithCenter:location2D radius:1 identifier:@"RegionIdentifier"];
[[Utils getLocationManager] startMonitoringForRegion:regionForMonitoring];

Now I have no idea what I have to do to fire local notifications with this information.

回答1:

I can tell you that your radius is more than likely too small to actually trigger an update. You have the radius set to 1 meter. That is going to take a location update almost too precise to register on anything other than testing coordinates you pass in.

Assuming you get a region event, I would put your local notification in the -didEnterRegion or -didExitRegion methods. You can create your notification like @Missaq said.

UILocalNotification *notification = [[UILocalNotification alloc] init]; 
notification.fireDate = [NSDate date]; 
NSTimeZone* timezone = [NSTimeZone defaultTimeZone]; 
notification.timeZone = timezone; 
notification.alertBody = @"Notification message"; 
notification.alertAction = @"Show"; 
notification.soundName = UILocalNotificationDefaultSoundName; 
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[notification release]; // release if not using ARC

Keep in mind that you will not get your notification if your application is active. You will have to be in background mode if you want to see your notification fire. If you application is active, you are expected to present an UIAlertView rather than UILocalNotification. Hope this helps.



回答2:

Try to use the

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region;

and

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region;

delegate methods.