通过提醒等本地通知进行地理定位

Mis*_*saq 1 reminders mkmapview cllocationmanager ios uilocalnotification

我想要实现地理定位通知,如应用提醒.这就是我已经做过的事情:

在App代表中:

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
{

}
Run Code Online (Sandbox Code Playgroud)

并且视图控制器中的这一个开始监视:

CLLocationCoordinate2D location2D = mapView.region.center; 
CLRegion *regionForMonitoring = [[CLRegion alloc] initCircularRegionWithCenter:location2D radius:1 identifier:@"RegionIdentifier"];
[[Utils getLocationManager] startMonitoringForRegion:regionForMonitoring];
Run Code Online (Sandbox Code Playgroud)

现在我不知道我需要做些什么才能使用这些信息来发送本地通知.

Bil*_*ess 5

我可以告诉你,你的半径可能太小而不能实际触发更新.您将半径设置为1米.这将使位置更新几乎过于精确,无法在测试您传入的坐标上进行注册.

假设你得到一个地区事件,我会把你的本地通知放在-didEnterRegion-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
Run Code Online (Sandbox Code Playgroud)

请注意,如果您的申请处于有效状态,您将无法收到通知.如果您想要查看通知,则必须处于后台模式.如果您的应用程序处于活动状态,则应该提供UIAlertView而不是UILocalNotification.希望这可以帮助.