How to monitor more than 20 regions using CLLocationManager

独自空忆成欢 提交于 2019-12-20 05:34:20

问题


I have about 2000 regions I want to monitor (entrance only) using CLLocationManager, I have a function that finds the 20 nearest stores (Store is a class that inherits from NSObject and has a CLLocationCoordinate2D property named geoPoint) to the user's current location but where should I call it?

If someone will be able to help me with a code example it'll be great!

Finding the 20 nearest stores func:

-(void)sortClosestStores
{
    [self.allStores sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
    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];

    float dist1 =[location1 distanceFromLocation:self.locationManager.location];
    float dist2 = [location2 distanceFromLocation:self.locationManager.location];
    if (dist1 == dist2) {
        return NSOrderedSame;
    }
    else if (dist1 < dist2) {
        return NSOrderedAscending;
    }
    else {
        return NSOrderedDescending;
    }
}];

    if (self.twentyClosestStores==nil) {
        self.twentyClosestStores=[NSMutableArray array];
    }

    if (self.previousTwentyStores==nil) {
        self.previousTwentyStores=[NSMutableArray array];
    }
    self.previousTwentyStores=self.twentyClosestStores;
    self.twentyClosestStores=[NSMutableArray array];

    for (int i = 0; i < 20; i++) {
        [self.twentyClosestStores addObject:[self.allStores objectAtIndex:i]];
    }
}

Thanks in advance!

来源:https://stackoverflow.com/questions/35132173/how-to-monitor-more-than-20-regions-using-cllocationmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!