Core data 使用核心数据绕过CLRegion 20区域上限的位置警报
我有一个将兴趣点(POI)存储在核心数据Core data 使用核心数据绕过CLRegion 20区域上限的位置警报,core-data,ios8,cllocationmanager,geofencing,Core Data,Ios8,Cllocationmanager,Geofencing,我有一个将兴趣点(POI)存储在核心数据managedObjectContext中的应用程序。我的目标是,如果currentLocation在managedObjectContext中的POI的指定范围内,则发出警报。在阅读有关CLRegion的文章时,似乎可以监控的区域数量上限为20个 为了绕过区域监控上限,我的游戏计划是浏览我的managedObjectContext以获得纬度/经度坐标,并在我的应用程序中每次触发我的位置管理器的didUpdateLocations时计算POI之间的距离:
managedObjectContext
中的应用程序。我的目标是,如果currentLocation
在managedObjectContext
中的POI的指定范围内,则发出警报。在阅读有关CLRegion
的文章时,似乎可以监控的区域数量上限为20个
为了绕过区域监控上限,我的游戏计划是浏览我的managedObjectContext
以获得纬度/经度坐标,并在我的应用程序中每次触发我的位置管理器的didUpdateLocations
时计算POI之间的距离:
- (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
}
}
-(无效)位置管理器:(CLLocationManager*)管理器更新位置:(NSArray*)位置{
CLLocation*location=[locations lastObject];
NSLog(@“LocationManager纬度%+.6f,经度%+.6f\n”,
位置坐标纬度,
位置、坐标、经度);
//TODO:计算managedObjectContext中对象之间的距离
[自行计算的情况];
self.currentLocation=位置;
}
//计算距离的方法
-(无效)计算状态{
//对于managedObjectContext中的POI,请执行以下操作
CLLocation*locA=[[CLLocation alloc]initWithLatitude:self.currentLocation.coordinate.latitude经度:self.currentLocation.coordinate.longitude];
CLLocation*locB=[[CLLocation alloc]initWithLatitude:POIVATION经度:POIVATION经度];
CLLocationDistance距离=[locA distanceFromLocation:locB];
如果(距离<1000){
//弹出警报
}
}
我只使用了
managedObjectContext
来显示数据。在本例中,我没有显示任何内容——相反,我只是在MOC
中运行对象,当didUpdateLocations
触发时,拉出坐标并计算距离。有没有办法做到这一点?我给我的类调用了LocationManager
,它是一个单例类。在标题中,我为NSManagedObjectContext
添加了一个属性
.h
@property (nonatomic, weak) NSManagedObjectContext *managedObjectContext;
我的核心数据堆栈在AppDelegate中,因此我调整了我的LocationManager
实现的init方法以“查看”它:
.m
- (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 CalculatedInstances]
的调用,如下所示:
- (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);
}
}
-(无效)位置管理器:(CLLocationManager*)管理器更新位置:(NSArray*)位置{
CLLocation*location=[locations lastObject];
NSLog(@“LocationManager纬度%+.6f,经度%+.6f\n”,
位置坐标纬度,
位置、坐标、经度);
[自行计算的情况];
self.currentLocation=位置;
}
//计算距离的方法
-(无效)计算状态{
n错误*错误;
NSFetchRequest*coordinateRetrieval=[[NSFetchRequest alloc]initWithEntityName:@“兴趣点”];
NSArray*POI=[self.managedObjectContext executeFetchRequest:coordinateRetrieval错误:&错误];
用于(兴趣点*poi中的地点){
CLLocation*locA=self.currentLocation;
CLLocation*locB=[[CLLocation alloc]initWithLatitude:[VICENUE.LATIONE doubleValue]经度:[VICENUE.LATIONE doubleValue]];
CLLocationDistance距离=[locA distanceFromLocation:locB];
如果(距离<1000){
//弹出警报
}
NSLog(@“当前位置到场馆的距离为%lf米”,距离);
}
}
我不确定我是否理解这个问题。如果您不想显示数据,则不要执行任何将其显示在屏幕上的代码。哪一部分没有意义?我更新了我的帖子以补充说明。