ios - startMonitoringFromRegion - 在 didEnterRegion 中做什么?

标签 ios ios4 core-location

我目前正在实现 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/

相关文章:

objective-c - 即使在 alloc/init 之后,我的 NSMutableArray 也会在函数范围之外丢失它的对象

iphone - 目前使用的每个 iOS 版本有多少台?

iphone - 形状的缩放和旋转(矩形)

ios - 当存在重叠的 CLRegion 时,地理围栏如何表现

ios - 如何为标题索引uitableview iOS的对象组成数组?

ios - Swift CoreData子实体无法通过父实体访问

ios - 有没有办法在没有用户或服务器干预的情况下唤醒 iOS 中暂停的应用程序

iphone - 核心数据:如何使用NSPredicate查找空字段

ios - AppDelegate 属性还是 Singleton 对象?

ios - iPhone 核心位置架构 - 如何跨多个 Controller 使用它