gpt4 book ai didi

objective-c - 未调用 CoreLocation 区域代表

转载 作者:行者123 更新时间:2023-12-01 16:56:56 25 4
gpt4 key购买 nike

我正在构建一个应用程序,特别是一个基于位置的应用程序,所以我需要让用户坐标经度和纬度及其工作。

现在我需要知道用户是否输入了我使用 CLLocationManager 预定义的区域。代表,所以我覆盖 didEnterRegionNSLog语句来知道用户是否进入了区域。


-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 

我调用 startMonitoringForRegion:但是像 didEnterRegion 这样的代表没有任何 react 或 monitoringDidFailForRegionCLRegion 的任何代表.

这是我的一些代码:
   -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
locLongittude=newLocation.coordinate.longitude;
locLattiude=newLocation.coordinate.latitude;

self.regionTimer=[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(monitorRegion) userInfo:nil repeats:NO];
}

-(void)startMonitor:(float)latitude longitude:(float)longitude radius:(float)radius
{
CLLocationCoordinate2D home;
home.latitude = latitude;
home.longitude = longitude;

CLRegion* region = [[CLRegion alloc] initCircularRegionWithCenter:home radius:radius identifier:@"home"];

if([CLLocationManager regionMonitoringEnabled] && [CLLocationManager regionMonitoringAvailable])
[locManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];

NSLog(@"Count: %d region aval %d enable %d",[[locManager monitoredRegions]count], [CLLocationManager regionMonitoringAvailable],[CLLocationManager regionMonitoringEnabled]);
for( CLRegion* region in locManager.monitoredRegions )
NSLog( @"%@", region );
}

-(void)monitorRegion
{
[self startMonitor:31.971160 longitude:35.832465 radius:10000.0];
}

编辑:使用模拟器完成所有测试并使用 Product-> Debug -> Simulate Location 模拟位置

好的,伙计们,它有效。上面的代码效果很好,问题是要使 didEnterRegion调用方法是在您需要输入的区域之外,然后输入它以使其工作。

我所做的是在模拟中,我总是在我需要输入的区域内选择一个坐标,这就是它必须在该区域之外的问题。

所以这是我的代码与您分享

更新:我想知道为什么当我离开该区域时没有调用 didExitRegion ?

最佳答案

根据我自己的经验,Region Monitoring 似乎依赖于 Cell Change 事件(为什么 Apple 会这样设计?我想是为了延长电池生命周期)。所以,这个实现并不理想。

理想情况下,只要您越过边界,它就应该可以工作,但在 iPhone 上却不会以同样的方式发生。即使使用内置的提醒应用程序,有时它也会通过基于位置的提醒在附近失败。

因此,在您的情况下,我认为单元格更改事件不会发生,这就是为什么不调用 didExitRegion 的原因。您可以使用 startMonitoringSignificantLocationChanges 仔细检查它。

关于objective-c - 未调用 CoreLocation 区域代表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10894600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com