我正在从事这样一个项目,其中应用程序执行以下操作:
- 用户选择一个半径(10 米到 1000 米)并按“Go”按钮转到下一个 viewController
- 这里的应用程序抓取用户当前位置并基于选定半径的当前位置开始“区域监控”
- 如果用户越过该特定边界(10 米到 1000 米),则它会发出“ExitRegion”警报消息。并根据用户新的当前位置再次启动“区域监控”。并且应用程序一直在这样做,无论是前台模式还是后台模式。我设法做到了,并且运行良好。
但是现在为了限制区域数量,通过“区域监控”进行监控,我想在创建新区域后删除每个“监控区域”。所以它应该像这样发生:
- 根据用户当前位置启动区域监控
- 退出特定区域并收到“退出区域”警报消息
stopMonitoringForRegion
从阵列中删除此“监控区域”- 根据用户当前位置重新开始区域监控
- 退出特定区域并收到“退出区域”警报消息
stopMonitoringForRegion
从阵列中删除此“监控区域”
它应该像这样继续下去。我正在尝试这个,但它不能正常工作。
这是我的代码:
-(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
了,这样它就不会尝试删除应用程序开头的“监控区域”。因为一开始它是NULL。如果有人能理解我的问题或有任何建议,请回复。先谢谢了。祝你今天过得愉快。