我正在做这样一个项目,应用程序可以做以下事情:
- 用户选择半径(10米到1000米),然后按"go"按钮进入下一个视图控制器
- 在这里,应用程序抓取用户的当前位置,并根据所选半径的当前位置开始"区域监控">
- 如果用户越过该特定边界(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: nn" 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。如果有人能理解我的问题或有任何建议,请回复。高级感谢。祝你今天愉快。
您试图从NSSet中删除第一个区域,但实际上NSSet是无序集合,所以在您的情况下这是不正确的。
[locationManager stopMonitoringForRegion:[[[locationManager monitoredRegions] allObjects] objectAtIndex:0]];
您必须遍历此集合才能找到您的区域或使用NSPredcate对其进行筛选,但为什么不在didExitRegion
方法中停止它呢?
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
// your code here
[manager stopMonitoringForRegion:region];
}
如苹果文档所述,关于监控区域NSSet:
此集合中的对象可能不一定与您在注册时指定。只有区域数据本身由系统维护。因此,唯一能够识别已注册的区域正在使用其标识符属性。
查看我如何在应用程序中注册/管理区域的示例:
- (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];
}];
}
}
并添加一个新区域:
NSString* locId = [NSString stringWithFormat:@"KCC: %@", [[NSUUID UUID] UUIDString]];
[self registerRegionWithCircularOverlay:circleOverlay andIdentifier:locId];
您必须找到一种使用标识符来管理它们的方法。