首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于用户当前位置区域监控,删除iOS7中的监控区域

基于用户当前位置区域监控,删除iOS7中的监控区域
EN

Stack Overflow用户
提问于 2014-02-25 07:55:34
回答 2查看 2.9K关注 0票数 2

我正在从事这样一个项目,在这个项目中,应用程序可以完成以下工作:

  1. 用户选择一个半径(10米到1000米),然后按下一个viewController按钮。
  2. 在这里,应用程序获取用户当前位置,并根据选定的radius当前位置启动“区域监视”。
  3. 如果用户穿越了特定的边界(10米到1000米),那么它就会发出"ExitRegion“警告信息。并根据用户新的当前位置再次启动“区域监控”。应用程序一直在这样做,无论是前景模式还是背景模式。我设法做到了&它运转得很好。

但现在,为了限制区域的数量,通过“区域监测”进行监测,我想在“区域监测”产生新的区域之后,删除每一个“监测区域”。所以应该是这样的:

  • 基于用户当前位置的启动区域监控
  • 退出特定区域&获取“退出区域”警报消息
  • stopMonitoringForRegion数组中删除此“监视区域”
  • 基于用户当前位置的重新启动区域监控
  • 退出特定区域&获取“退出区域”警报消息
  • stopMonitoringForRegion数组中删除此“监视区域”

它应该这样继续下去。我正在尝试,但它没有正常工作。

这是我的代码:

代码语言:javascript
复制
-(void)startLocationServices
{
    if (self.locationManager == nil)
    {
        self.locationManager = [CLLocationManager new];
    }
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [self.locationManager setDelegate:self];
    [self.locationManager setDistanceFilter:kCLDistanceFilterNone];
    //[self.locationManager startUpdatingLocation];
}

-(void) monitoringRegion
{
    if (flag == 0)
    {
        if (flagForRemovingRegion == 1)
        {
            // Remove monitored region from "monitoredRegions" array after monitor 5 regions
            [self removingMonitoredRegion];
        }

        CLLocationCoordinate2D center = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude);

        CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:myval identifier:@"Test"];
        CLLocationDegrees radius = myval;

        if (radius > self.locationManager.maximumRegionMonitoringDistance)
        {
            radius = self.locationManager.maximumRegionMonitoringDistance;
        }
        [self.locationManager startMonitoringForRegion:region];

        flag = 1;
        flagForRemovingRegion = 1;
        self.availabilityTextView.text = [@"Your selected Radius:" stringByAppendingFormat:@"%i", self.myval];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    flag = 0;
    [self startLocationServices];
    [self monitoringRegion];
}

-(void) removingMonitoredRegion
{
    [locationManager stopMonitoringForRegion:[[[locationManager monitoredRegions] allObjects] objectAtIndex:0]];


}

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
//    // regions are stored by system
    self.threeTextView.text = [@"Regions: \n\n" stringByAppendingFormat:@"%@", [[self.locationManager monitoredRegions] allObjects]];

    UIAlertView *alertViewOne = [[UIAlertView alloc] initWithTitle:@"Status" message:@"Region Monitoring started." delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];

    [alertViewOne show];
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    UIAlertView *alertViewTwo = [[UIAlertView alloc] initWithTitle:@"Status" message:@"You Enter the region" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];

    [alertViewTwo show];
    self.availabilityTextView.text = @"You enter the region!";
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    UIAlertView *alertViewThree = [[UIAlertView alloc] initWithTitle:@"Status" message:@"You Exit the region" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];

    [alertViewThree show];
    flag = 0;
    self.availabilityTextView.text = @"You exit the region!";
    [self monitoringRegion];
}

- (void) locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
    self.availabilityTextView.text = [@"\nError:" stringByAppendingFormat:@"%@", [error localizedDescription]];
}

我设置了flagForRemovingRegion,这样它就不会尝试删除应用程序开始时的“监视区域”。因为在一开始它是空的。如果有人能理解我的问题或有任何建议,请回复。在高级版谢谢。祝您今天愉快。

EN

回答 2

Stack Overflow用户

发布于 2014-02-25 08:08:55

您尝试从NSSet中删除第一个区域,但实际上NSSet是无序集合,因此在您的情况下是不正确的。

代码语言:javascript
复制
[locationManager stopMonitoringForRegion:[[[locationManager monitoredRegions] allObjects] objectAtIndex:0]];

您必须遍历这个集合才能找到您的区域或使用NSPredicate对其进行筛选,但是为什么不在didExitRegion方法中停止它呢?

代码语言:javascript
复制
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    // your code here

    [manager stopMonitoringForRegion:region];
}
票数 1
EN

Stack Overflow用户

发布于 2014-02-25 08:13:57

正如苹果文档中关于受监控区域的声明一样,NSSet:

此集合中的对象可能不一定是在注册时指定的相同对象。只有区域数据本身由系统维护。因此,惟一标识已注册区域的唯一方法是使用其标识符属性。

请参阅我如何在应用程序中注册/管理我的区域的示例:

代码语言:javascript
复制
- (void)registerRegionWithCircularOverlay:(MKCircle*)overlay andIdentifier:(NSString*)identifier {

    // If the overlay's radius is too large, registration fails automatically,
    // so clamp the radius to the max value.
    CLLocationDegrees radius = overlay.radius;
    if (radius > sharedInst.locationManager.maximumRegionMonitoringDistance) {
        radius = sharedInst.locationManager.maximumRegionMonitoringDistance;
    }
    // Create the geographic region to be monitored.
    CLCircularRegion *geoRegion = [[CLCircularRegion alloc]
                                   initWithCenter:overlay.coordinate
                                   radius:radius
                                   identifier:identifier];
    if([CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]])
        if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized){
            NSLog(@"we can monitor");
            Region* reg = [[Region alloc] init];
            reg.myRegion = geoRegion;
            [sharedInst.regionsDict setObject:reg forKey:identifier];
            [sharedInst.locationManager startMonitoringForRegion:geoRegion];

            CLGeocoder *coder = [[CLGeocoder alloc]init] ;
            CLLocation *myLocation = [[CLLocation alloc]initWithLatitude:geoRegion.center.latitude longitude:geoRegion.center.longitude];

            [coder reverseGeocodeLocation:myLocation completionHandler:
             ^(NSArray *placemarks, NSError *error){
                 CLPlacemark *placemark= [placemarks objectAtIndex:0];
                 reg.myName = [NSString stringWithFormat:@"%@, %@", placemark.locality, placemark.thoroughfare];
                 NSLog(@"we did monitor: %@", reg.myName);
                 [sharedInst saveData];
             }];

        }
}

并增加一个新的区域:

代码语言:javascript
复制
   NSString* locId = [NSString stringWithFormat:@"KCC: %@", [[NSUUID UUID] UUIDString]];
   [self registerRegionWithCircularOverlay:circleOverlay andIdentifier:locId];

您必须找到一种使用标识符来管理它们的方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22007889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档