1

我想实现像应用提醒这样的地理位置通知。这是我已经做过的:

在应用委托中:

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
{

}

而视图控制器中的这个女巫开始监控:

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

现在我不知道如何使用这些信息触发本地通知。

4

2 回答 2

5

我可以告诉你,你的半径很可能太小而无法真正触发更新。您将半径设置为 1 米。这将需要一个几乎太精确的位置更新来注册除了测试你传入的坐标之外的任何东西。

假设您收到一个区域事件,我会将您的本地通知放在-didEnterRegionor-didExitRegion方法中。您可以像@Missaq 所说的那样创建您的通知。

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

请记住,如果您的应用程序处于活动状态,您将不会收到通知。如果您想看到通知触发,您必须处于后台模式。如果您的应用程序处于活动状态,您应该提交一个UIAlertView而不是UILocalNotification. 希望这可以帮助。

于 2012-01-30T15:00:30.910 回答
2

尝试使用

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

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

委托方法。

于 2012-01-30T12:34:12.333 回答