我目前正在实现 startMonitoringFromRegion:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
self.currentLocation = newLocation;
if (![CLLocationManager regionMonitoringAvailable] ||
![CLLocationManager regionMonitoringEnabled])
return;
CLLocationDegrees radius = 5;
if (radius > self.locationManager.maximumRegionMonitoringDistance)
radius = self.locationManager.maximumRegionMonitoringDistance;
CLRegion* region = [[CLRegion alloc] initCircularRegionWithCenter:self.currentLocation.coordinate
radius:radius
identifier:@"Indentifier"];
[self.locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyHundredMeters];
[region release];
}
当我进入一个新区域时,didEnterRegion 将被调用。 我的问题是,我应该在 didEnterRegion 中做什么?
我有一个包含所有坐标的数组。我应该提取region.center并将该坐标与我的坐标数组进行比较,然后查看哪一个坐标最接近region.center?
最佳答案
我注意到 CLRegion 有一个很棒的方法,称为 containsCooperative。
因此,我现在可以只使用 containsCoordinate,而不是循环遍历 didEnterRegion 中的所有麦当劳数组坐标并检查它们距 Region.center 的距离是否小于 x 公里。
for (Restaurant *restaurant in arrRestaurants)
{
CLLocation *restaurantLocation = [[CLLocation alloc] initWithLatitude:[resturant.latitude doubleValue] longitude:[restraurant.longitude doubleValue]];
if ([region containsCoordinate: restaurantLocation.coordinate])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"McDonads found in the region!" message:[NSString stringWithFormat:@"%@", restaurant.town] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
现在,我还没有尝试过,但对我来说这似乎是合乎逻辑的。
关于ios - startMonitoringFromRegion - 在 didEnterRegion 中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7147942/