Ios CLLocationAccuracyBest]; [新地区发布]; } 否则{ NSLog(@“区域监控不可用”); } }
这项工作源自“区域”模板。这是您将获得的最佳精度。区域匹配仅使用细胞塔三角测量,因此在密集区域,它可能在50-100米处触发,而在稀疏区域,它可能需要100米Ios CLLocationAccuracyBest]; [新地区发布]; } 否则{ NSLog(@“区域监控不可用”); } },ios,geolocation,location,ios-simulator,geofencing,Ios,Geolocation,Location,Ios Simulator,Geofencing,这项工作源自“区域”模板。这是您将获得的最佳精度。区域匹配仅使用细胞塔三角测量,因此在密集区域,它可能在50-100米处触发,而在稀疏区域,它可能需要100米 如果您想要更详细的信息,那么您将不得不使用直接定位服务。但这会消耗电池,所以选择你的毒药。iOS模拟器对于带有退出/进入功能的geofence更新是出了名的糟糕。您可以确认这一点的一种方法是在应用程序上设置geofence作为您的当前位置。然后,在iOS模拟器的调试菜单中,按location,然后按Freeway Drive。您的设备现在
如果您想要更详细的信息,那么您将不得不使用直接定位服务。但这会消耗电池,所以选择你的毒药。iOS模拟器对于带有退出/进入功能的geofence更新是出了名的糟糕。您可以确认这一点的一种方法是在应用程序上设置geofence作为您的当前位置。然后,在iOS模拟器的调试菜单中,按location,然后按Freeway Drive。您的设备现在将模拟在高速公路上行驶,如果您的模拟器与我的一样,它将不会注册didExitRegion事件 为什么??区域主要由苹果的隐藏算法决定,该算法使用Wi-Fi、手机发射塔和手机上请求位置的其他应用程序。因为模拟器不使用Wi-Fi或手机发射塔。。。区域监控将是非常不可能的
要测试区域退出后代码的其余部分是否正常工作,您可以创建一个按钮或其他东西,手动触发locationManager:didExitRegion:method。我还强烈建议在您的设备上编译该项目,方法是将其插入并从iPhone模拟器更改为iOS设备。然后,您可以拔下设备的插头,从手机上运行应用程序进行测试。我在iPhone上部署了应用程序,设置了一个15-25米的区域,在该区域外走了整整50米,什么都没有。有什么建议吗?
- (void)viewDidLoad {
[super viewDidLoad];
// Create empty array to add region events to.
updateEvents = [[NSMutableArray alloc] initWithCapacity:0];
// Create location manager with filters set for battery efficiency.
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLLocationAccuracyBest; //could try KLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// Start updating location changes.
[locationManager startUpdatingLocation];
}
- (void)viewDidAppear:(BOOL)animated {
// Get all regions being monitored for this application.
NSArray *regions = [[locationManager monitoredRegions] allObjects];
// Iterate through the regions and add annotations to the map for each of them.
for (int i = 0; i < [regions count]; i++) {
CLRegion *region = [regions objectAtIndex:i];
RegionAnnotation *annotation = [[RegionAnnotation alloc] initWithCLRegion:region];
[regionsMapView addAnnotation:annotation];
[annotation release];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"didFailWithError: %@", error);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);
// Work around a bug in MapKit where user location is not initially zoomed to.
if (oldLocation == nil) {
// Zoom to the current user location.
MKCoordinateRegion userLocation = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 100.0, 100.0);
[regionsMapView setRegion:userLocation animated:YES];
}
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSString *event = [NSString stringWithFormat:@"didEnterRegion %@ at %@", region.identifier, [NSDate date]];
[self updateWithEvent:event];
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSString *event = [NSString stringWithFormat:@"didExitRegion %@ at %@", region.identifier, [NSDate date]];
[self updateWithEvent:event];
//implement local notification:
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notification = [[UILocalNotification alloc] init];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
if (notification == nil)
return;
notification.alertBody = [NSString stringWithFormat:@"Did You Lock Your House?"];
notification.alertAction = @"Lock House";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[app presentLocalNotificationNow:notification];
[notification release];
// ends here
//following is an alert for the case of exiting boundary whilst app is in foreground
UIAlertView *alr=[[UIAlertView alloc] initWithTitle:@"Reminder didExitRegion" message:region.identifier delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok",nil];
[alr show];
[alr release];
//ends here
}
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error {
NSString *event = [NSString stringWithFormat:@"monitoringDidFailForRegion %@: %@", region.identifier, error];
[self updateWithEvent:event];
}
- (IBAction)addRegion {
if ([CLLocationManager regionMonitoringAvailable]) {
// Create a new region based on the center of the map view.
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(regionsMapView.centerCoordinate.latitude, regionsMapView.centerCoordinate.longitude);
CLRegion *newRegion = [[CLRegion alloc] initCircularRegionWithCenter:coord
radius:25.0
identifier:[NSString stringWithFormat:@"%f, %f", regionsMapView.centerCoordinate.latitude, regionsMapView.centerCoordinate.longitude]];
// Create an annotation to show where the region is located on the map.
RegionAnnotation *myRegionAnnotation = [[RegionAnnotation alloc] initWithCLRegion:newRegion];
myRegionAnnotation.coordinate = newRegion.center;
myRegionAnnotation.radius = newRegion.radius;
[regionsMapView addAnnotation:myRegionAnnotation];
[myRegionAnnotation release];
// Start monitoring the newly created region.
[locationManager startMonitoringForRegion:newRegion desiredAccuracy:kCLLocationAccuracyBest];
[newRegion release];
}
else {
NSLog(@"Region monitoring is not available.");
}
}