首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >地理位置不发送通知(iOS)

地理位置不发送通知(iOS)
EN

Stack Overflow用户
提问于 2016-01-20 15:57:09
回答 1查看 161关注 0票数 0

每次用户通过我的客户端的存储(大约1700个商店)时,我都试图发送一个通知,但它不起作用。

谁能告诉我原因吗?

我的代码:

Store.m

代码语言:javascript
运行
复制
-(CLCircularRegion*)createCircularRegion
{
    CLCircularRegion *region=[[CLCircularRegion alloc] initWithCenter:self.geoPoint radius:1000 identifier:self.identifier];

    region.notifyOnEntry=YES;

    return region;
}

viewController.m

代码语言:javascript
运行
复制
-(void)startMonitoringAllStores
{
    if (![CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) {
        NSLog(@"Monitoring is not available for CLCircularRegion class");
    }

    for (Store *currentStore in self.allStores) {
        CLCircularRegion *region=[currentStore createCircularRegion];
        [self.locationManager startMonitoringForRegion:region];
    }
}

-(void)getStoresFromParse
{
    [self findObjectsWithClassName:@"Stores"];

    [self.allStores addObjectsFromArray:self.allStores];

    [self startMonitoringAllStores];
}

AppDelegate.m

代码语言:javascript
运行
复制
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.locationManager.delegate=self;
    [self.locationManager requestAlwaysAuthorization];

    return YES;
}


-(void)handleRegionEvent:(CLRegion*)region
{
    NSLog(@"Geofence triggered");
}


-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    if ([region isKindOfClass:[CLCircularRegion class]]) {
        [self handleRegionEvent:region];
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-01-20 16:05:37

在iOS中,您只能同时监视20个位置。见苹果的文档。另外,确保您正在通过requestAlwaysAuthorization请求许可。

绕过这一限制的一种方法是找到最接近用户的19家商店并监视这些区域,然后使用20区域作为用户当前位置周围的外部边界,其半径是到第19商店的距离。当用户越过外部边界时,重新计算最近的19个存储和边界,然后重复。在代码中,你会做这样的事情。(请注意,我还没有测试过这段代码;它只是一个示例。)

代码语言:javascript
运行
复制
- (void)installClosestGeofencesTo:(CLLocation *)currentLocation {
    [self removeAllGeofences];
    NSArray *closestStores = [self findClosestStoresTo:currentLocation limit:19];
    for (Store *store in closestStores) {
        CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:store.location radius:100 identifier:store.identifier];
        [self.locationManager startMonitoringForRegion:region];
    }
    Store *furthestStore = [closestStores lastObject];
    CLCircularRegion *boundaryRegion = [[CLCircularRegion alloc] initWithCenter:currentLocation.coordinate radius:furthestStore.distance identifier:@"boundary"];
    [self.locationManager startMonitoringForRegion:boundaryRegion];
}

- (void)removeAllGeofences {
    NSArray *monitoredRegions = [self.locationManager monitoredRegions];
    for (CLRegion *region in monitoredRegions) {
        [self.locationManager stopMonitoringForRegion:region];
    }
}

- (NSArray *)findStoresClosestTo:(CLLocation *)location limit:(NSUInteger)limit {
    ...
}

在您的CLLocationManagerDelegate中,您可以这样做:

代码语言:javascript
运行
复制
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    if ([region.identifier isEqual:@"boundary"]) {
        [self installClosestGeofencesTo:self.locationManager.location];
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34904674

复制
相关文章

相似问题

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