ios – CLLocationManager monitoredRegions(NSSet)不正确,还是其他什么?

ios – CLLocationManager monitoredRegions(NSSet)不正确,还是其他什么?,第1张

概述我想检查每次用户从一系列商店通过商店时,我有超过20个商店,所以我写了一个功能,找到20个最近的商店到用户的位置并监控它们.在locationManager上更新的列表:didUpdateLocations,我还用新的20个最近的商店位置替换旧的20个受监控区域. 问题是当用户进入区域时,不会定期调用locationManager:didEnterRegion. 我也注意到,当我检查locatio 我想检查每次用户从一系列商店通过商店时,我有超过20个商店,所以我写了一个功能,找到20个最近的商店到用户的位置并监控它们.在locationManager上更新的列表:dIDUpdateLocations,我还用新的20个最近的商店位置替换旧的20个受监控区域.

问题是当用户进入区域时,不会定期调用locationManager:dIDEnterRegion.

我也注意到,当我检查locationManager.monitoredRegions NSSet时,由于某些原因,我检查了区域是错误的(我检查了句子,所以也许它们是正确的,只是更短?).

如果有人可以检查我的代码并且可能注意到我做错了什么,它真的会帮助我!

我的代码:

monitorLocationVIEwController.m(滚动查看完整代码):

-(voID)getStoresArrays:(NSNotification*)notification{    //Fetching "allStoresArray"(NSArray of all the stores sent from another class using NSNotificationCenter) and adding it to "allStores"(NSMutableArray)    NSDictionary *storesCategorIEsArrays=[notification userInfo];    self.allStores=[storesCategorIEsArrays objectForKey:@"allStoresArray"];    //Calling "locationChangeHandler" for monitoring    [self locationChangeHandler];}#pragma mark - CLLocationDelegate methods//Being called when user's location updated-(voID)locationManager:(CLLocationManager *)manager dIDUpdateLocations:(NSArray<CLLocation *> *)locations{    //If "allStores"(NSMutableArray) isn't nil - calling "locationChangeHandler" to update monitoring    if (self.allStores!=nil) {        [self locationChangeHandler];    }}//Being called when user enters a monitored region-(voID)locationManager:(CLLocationManager *)manager dIDEnterRegion:(CLRegion *)region{    NSLog(@"Entered");}#pragma mark - Closest stores sorting methods//Sorting closest stores to the user's location and adding the 20 closest store to "twentyClosestStores"(NSMutableArray)-(voID)sortClosestStores{    //Sorting "allStores"(NSMutableArray) from the closest "Store" to the furthest.    [self.allStores sortUsingComparator:^NSComparisonResult(ID  _Nonnull obj1,ID  _Nonnull obj2) {        //Creating "location1"(CLLocation) and "location2"(CLLocation) and initializing each with "obj1"(ID) and "obj2"(ID) coordinates        CLLocation *location1=[[CLLocation alloc] initWithLatitude:((Store*)obj1).geoPoint.latitude longitude:((Store*)obj1).geoPoint.longitude];        CLLocation *location2=[[CLLocation alloc] initWithLatitude:((Store*)obj2).geoPoint.latitude longitude:((Store*)obj2).geoPoint.longitude];        //Creating "dist1"(float) and setting its value to the distance between "location1"(CLLocation) and the user's location        float dist1 =[location1 distanceFromLocation:self.locationManager.location];        //Creating "dist2"(float) and setting its value to the distance between "location2"(CLLocation) and the user's location        float dist2 = [location2 distanceFromLocation:self.locationManager.location];        //If the distances are equal - the order will stay the same        if (dist1 == dist2) {            return NSOrderedSame;        }        else if (dist1 < dist2) { //If "dist1"(float) is smaller than "dist2"(float) - "dist2"(float) will be before "dist1" in the array            return NSOrderedAscending;        }        else { //else - "dist2"(float) will be before "dist1" in the array            return NSOrderedDescending;        }    }];    //If "twentyClosestStores"(NSMutableArray) is nil    if (self.twentyClosestStores==nil) {        //Initializing "twentyClosestStores"(NSMutableArray)        self.twentyClosestStores=[NSMutableArray array];    }    //If "prevIoUsTwentyStores"(NSMutableArray) is nil    if (self.prevIoUsTwentyStores==nil) {        //Initializing "prevIoUsTwentyStores"(NSMutableArray)        self.prevIoUsTwentyStores=[NSMutableArray array];    }    //Setting "prevIoUsTwentyStores"(NSMutableArray) to "twentyClosestStores"(NSMutableArray)    self.prevIoUsTwentyStores=self.twentyClosestStores;    //Cleaning (reInitializing) "twentyClosestStores"(NSMutableArray)    self.twentyClosestStores=[NSMutableArray array];    //Adding indexes 0-19 of "allStores"(NSMutableArray) (20 closest stores to the user's current location) to "twentyClosestStores"(NSMutableArray)    for (int i = 0; i < 20; i++) {        [self.twentyClosestStores addobject:[self.allStores objectAtIndex:i]];    }}#pragma mark - Start/stop monitoring methods//For updating monitoring-(voID)locationChangeHandler{    //If "allStores"(NSMutableArray) isn't nil    if (self.allStores!=nil) {        //Finding the 20 closest stores to he user's location and adding it to "twentyClosestStores"(NSMutableArray)        [self sortClosestStores];        //Stop monitoring "prevIoUsTwentyStores"(NSMutableArray) (20 closest stores before user's location  updated)        [self stopMonitoringStores];        //Start monitoring "twentyClosestStores"(NSMutableArray)        [self startMonitoringClosestStores];    }}//Start monitoring "twentyClosestStores"(NSMutableArray)-(voID)startMonitoringClosestStores{    //If monitoring isn't availible for "CLCircularRegion"    if (![CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) {        NSLog(@"Monitoring is not available for CLCircularRegion class");    }    //Run on all "twentyClosestStores"(NSMutableArray)'s objects    for (Store *currentStore in self.twentyClosestStores) {        //Creating "region"(CLCircularRegion) and setting it to "currentStore"(Store)'s circular region        CLCircularRegion *region=[currentStore createCircularRegion];        //Start monitoring "region"(CLCircularRegion)        [self.locationManager startMonitoringForRegion:region];    }}//Stop monitoring "prevIoUsTwentyStores"(NSMutableArray) (20 closest stores before user's location  updated)-(voID)stopMonitoringStores{    //Run on all "prevIoUsTwentyStores"(NSMutableArray)'s objects    for (Store *currentStore in self.prevIoUsTwentyStores) {        //Creating "region"(CLCircularRegion) and setting it to "currentStore"(Store)'s circular region         CLCircularRegion *region=[currentStore createCircularRegion];        //Stop monitoring "region"(CLCircularRegion)        [self.locationManager stopMonitoringForRegion:region];    }}//Finding a store for region-(Store*)storeForRegion:(CLCircularRegion*)region{    //Creating "latitude"(CGfloat) and "longtitude"(CGfloat) and setting it to "region"(CLCircularRegion)'s center.latitude/longtitude    CGfloat latitude=region.center.latitude;    CGfloat longtitude=region.center.longitude;    //Run on all "allStores"(NSMutableArray)'s objects    for (Store *currentStore in self.allStores) {        //If "currentStore"(Store)'s latitude and longtitude is equal to "latitude"(CGfloat) and longtitude(CGfloat)        if (currentStore.geoPoint.latitude==latitude&&currentStore.geoPoint.longitude==longtitude) {            //Returning "currentStore"(Store)            return currentStore;        }    }    //Store not found - returning nil    NSLog(@"No store found for this region: %@",[region description]);    return nil;}

Store.m:

//Creating and returning a "CLCircularRegion" object of the store-(CLCircularRegion*)createCircularRegion{    //Creating "region"(CLCircularRegion) and initializing it with current store information (self) and radios of 200m    CLCircularRegion *region=[[CLCircularRegion alloc] initWithCenter:self.geoPoint radius:200 IDentifIEr:self.IDentifIEr];    //Setting "region"(CLCircularRegion)'s notifyOnEntry to YES    region.notifyOnEntry=YES;    //Returning "region"(CLCircularRegion)    return region;}

注意:正在调用委托方法,甚至是dIDEnterRegion:但并不总是出于某种原因.

解决了这个问题:

(我决定不使用区域监控并自己做)

-(voID)locationManager:(CLLocationManager *)manager dIDUpdateLocations:(NSArray<CLLocation *> *)locations{    [self checkIfNearStore]; //Not being called in background}-(voID)checkIfNearStore    {        for (Store *currentStore in self.allStores) {            if ([currentStore.circularRegion containsCoordinate:self.locationManager.location.coordinate]&&currentStore.alreadySendNotification==NO) {                NSLog(@"Entered: %@",[[self storeForRegion:currentStore.circularRegion] address]);                currentStore.alreadySendNotification=YES;                [self.storesAlreadySentNotifications addobject:currentStore];            }        }        for (Store *currentStore in self.storesAlreadySentNotifications) {            if (![currentStore.circularRegion containsCoordinate:self.locationManager.location.coordinate]) {                currentStore.alreadySendNotification=NO;            }        }    }

非常感谢你!

解决方法 区域半径

请查看下面有关startMonitoringForRegion方法的讨论,并注意Apple指出最多400个区域的值在以后的设备上效果最佳.他们还指出,人们可以在平均3到5分钟内得到答复.我不确定你是如何测试该地区的出入口,但可能是你没有足够的时间通过.

区域监测

另外,我建议您设置断点,以检查代码中策略点的CLLocationManager的monitoredRegions属性(当您希望更新区域时).在使用自定义代码重新填充之前,您可以考虑清空regions数组(请参阅下面的namely:标题).我会仔细检查那个区域,以确定预期的行为正在发生.

过滤器

当位置发生重大变化时,有许多过滤器告诉CLLocationManager.您应该设置这些值并检查它们是否会对更新频率产生影响.另请注意我展示的其他属性和方法,包括desiredAccuracy和启动重要更新的方法.阅读每个讨论,看看它们是否适用于您的用例.尝试在需要时应用更改,以便在适合您的情况下强制设备更新.

点击测试用户位置

您可能希望使用用户位置实现特定CLCircularRegion的定期命中测试.这应该允许您在代码中的预定时间/位置进一步细化位置更新.例如,您可能希望使用精度和距离过滤器优化用户更新,然后在委托方法中调用针对阵列中最近区域的查询.净效果是在设备从最后的已知位置显着移动时强制进行区域状态检查.

位置更改的后台更新

我建议您仔细阅读与startMonitoringSignificantLocationChanges方法相关的讨论(您必须使用此方法来解决此问题). Apple详细介绍了在应用程序处于后台时获取更新所需的步骤.基本上,您必须在应用委托方法中拦截一个键.幸运的是,SO question有示例代码.但在您跳转到代码之前,请按照建议进行讨论,因为它会让您更清楚地了解如何掌握此行为.

部分答案

像许多问题一样.这个需要一些部分答案来解决您需要的功能.首先确保您已经检查了上面提供的注释,然后尝试使用这些链接来获得特定所需任务的部分答案.

How can I disable any CLRegion objects registered with -startMonitoringForRegion?

即:

Useful responses on how to refine geofencing

A simplified tutorial of geofencing

(您是否在位置管理器上使用了requestAlwaysAuthorization()?)

Nota Bene

我建议您阅读方法和属性的讨论,以查看Apple的提示.您还应该可以自由地单击代码编辑器中的方法和属性,并查看头文件中的注释.这些将提供Apple提供的更多信息,这些信息在文档或快速帮助中不可用.

作为一个简单的示例,以下标题注释似乎与您的目标相关.您可能希望实现并监视注释标头中提到的委托方法和requestStateForRegion方法:

/* *  locationManager:dIDDetermineState:forRegion: * *  discussion: *    Invoked when there's a state Transition for a monitored region or in response to a request for state via a *    a call to requestStateForRegion:. */@available(iOS 7.0,*)optional public func locationManager(manager: CLLocationManager,dIDDetermineState state: CLRegionState,forRegion region: CLRegion)
总结

以上是内存溢出为你收集整理的ios – CLLocationManager monitoredRegions(NSSet)不正确,还是其他什么?全部内容,希望文章能够帮你解决ios – CLLocationManager monitoredRegions(NSSet)不正确,还是其他什么?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1029479.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存