我有一个要求,当用户从该点越过半径时,我必须向用户显示警报视图。
如何设置半径?
最佳答案
首先,您需要使用以下方法创建区域:
CLLocationDegrees latitude = <YOUR_LATITUDE>;
CLLocationDegrees longitude = <YOUR_LONGITUDE>;
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(latitude, longitude);
CLLocationDistance radius = GEO_FENCE_RADIUS;
CLRegion *region = [[CLRegion alloc]initCircularRegionWithCenter:center radius:radius identifier:title];
接下来,您必须通过以下方式监视区域:
[locationManager startMonitoringForRegion:region];
然后,您可以通过以下委托方法跟踪用户进入或退出的时间:
- (void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region {
//Show your alert as they are entered the region
}
- (void)locationManager:(CLLocationManager *)manager
didExitRegion:(CLRegion *)region {
//Show your alert as they are exited the region
}
- (void)locationManager:(CLLocationManager *)manager
didStartMonitoringForRegion:(CLRegion *)region {
}
- (void)locationManager:(CLLocationManager *)manager
monitoringDidFailForRegion:(CLRegion *)region
withError:(NSError *)error {
}