【问题标题】:Geolocation with local notification like reminder带有本地通知(如提醒)的地理位置
【发布时间】:2012-01-30 12:07:29
【问题描述】:

我想实现地理定位通知,例如应用提醒。 这是我已经做过的:

在应用委托中:

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];

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

【问题讨论】:

    标签: ios mkmapview cllocationmanager uilocalnotification reminders


    【解决方案1】:

    我可以告诉您,您的半径很可能太小而无法真正触发更新。您将半径设置为 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
    

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

    【讨论】:

    • 我正在挖掘这个主题。使用这种方法,方法didEnterRegion 仅在您的应用程序处于唤醒状态时才会触发。即使您的应用程序没有启动,是否有可能获得地理定位通知?
    • 无论你的应用是在前台还是后台,都应该调用这些方法。如果您没有从后台收到这些电话,那么您的位置管理器控制器可能无法正常工作。
    【解决方案2】:

    尝试使用

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

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

    委托方法。

    【讨论】:

    • 我必须在这些方法中做什么?
    • 好吧 - 创建本地通知?
    • 这样:- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { UILocalNotification *notif = [[UILocalNotification alloc] init]; notif.fireDate = [NSDate date]; NSTimeZone* tz = [NSTimeZone defaultTimeZone]; notif.timeZone = tz; notif.alertBody = @"Did enter Region"; notif.alertAction = @"Show"; notif.soundName = UILocalNotificationDefaultSoundName; [[UIApplication sharedApplication] scheduleLocalNotification:notif]; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    相关资源
    最近更新 更多