首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用核心数据获取CLRegion 20区域上限的位置警报

使用核心数据获取CLRegion 20区域上限的位置警报
EN

Stack Overflow用户
提问于 2015-03-25 22:05:08
回答 1查看 273关注 0票数 0

我有一个在核心数据managedObjectContext中存储感兴趣点(POI)的应用程序。我的目标是,如果currentLocationmanagedObjectContext中POI的指定范围内,就会发出警报。在阅读有关CLRegion的文章时,可以监视的区域的数量似乎限制在20个。

为了绕过区域监控帽,我的游戏计划是在每次我的位置管理器的managedObjectContext在我的应用程序中触发时,浏览我的didUpdateLocations中的纬度/经度坐标,并计算POI之间的距离:

代码语言:javascript
复制
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *location = [locations lastObject];
    NSLog(@"LocationManager Latitude %+.6f, Longitude %+.6f\n",
          location.coordinate.latitude,
          location.coordinate.longitude);
    // TODO: compute distances between objects in managedObjectContext
    [self calculateDistances];
    self.currentLocation = location;
}

// method to compute distances
- (void) calculateDistances {

    // for POI in managedObjectContext, do the following
    CLLocation *locA = [[CLLocation alloc] initWithLatitude:self.currentLocation.coordinate.latitude longitude:self.currentLocation.coordinate.longitude];

    CLLocation *locB = [[CLLocation alloc] initWithLatitude:POIlatitude longitude:POIlongitude];

    CLLocationDistance distance = [locA distanceFromLocation:locB];

    if (distance < 1000) {
        // popup alert
    }

}

我只使用managedObjectContext来显示数据。在本例中,我没有显示任何内容--而是在运行我的MOC中的对象时,didUpdateLocations触发、提取坐标和计算距离。有什么办法做到这一点吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-26 18:47:15

我调用了我的类LocationManager,这是一个单例类。在标头中,我为NSManagedObjectContext添加了一个属性。

.h

代码语言:javascript
复制
@property (nonatomic, weak) NSManagedObjectContext *managedObjectContext;

我的核心数据堆栈位于我的AppDelegate中,因此我调整了LocationManager实现的init方法,以“查看”它:

.m

代码语言:javascript
复制
- (id)init {
    self = [super init];
    if (!self.locationManager) {
        // This is so I can get a reference to the managedObjectContext
        AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        self.managedObjectContext = appDelegate.managedObjectContext;

        self.locationManager = [[CLLocationManager alloc]init];

        if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [self.locationManager requestAlwaysAuthorization];
        }

        self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
        self.locationManager.distanceFilter = 100; // meters
        self.locationManager.delegate = self;
    }
    return self;
}

接下来,当didUpdateLocations方法触发时,我添加了一个对[self calculateDistances]的调用,如下所示:

代码语言:javascript
复制
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *location = [locations lastObject];
    NSLog(@"LocationManager Latitude %+.6f, Longitude %+.6f\n",
          location.coordinate.latitude,
          location.coordinate.longitude);
    [self calculateDistances];
    self.currentLocation = location;
}

// method to compute distances
- (void) calculateDistances {

    NSError *error;
    NSFetchRequest *coordinateRetrieval = [[NSFetchRequest alloc]initWithEntityName:@"PointOfInterest"];
    NSArray *pois = [self.managedObjectContext executeFetchRequest:coordinateRetrieval error:&error];

    for (PointOfInterest *venue in poi) {
        CLLocation *locA = self.currentLocation;
        CLLocation *locB = [[CLLocation alloc]initWithLatitude:[venue.latitude doubleValue] longitude:[venue.longitude doubleValue]];
        CLLocationDistance distance = [locA distanceFromLocation:locB];

        if (distance < 1000) {
            // popup alert
        }
        NSLog(@"The distance from currentLocation to venue is %lf meters", distance);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29267438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档