1

我允许用户设置一个位置,然后创建一个区域并监控用户是否退出或进入了该区域。我还不断监控用户与所选位置的距离。区域监控被调用,距离被正确更新,但从不调用进入和退出。这表明我要去 30 米,但我走得更远,结果相同。

-(void)setPlugLocation:(CLLocation *)plugLocation{
if (!_plugLocation) {
    _plugLocation=plugLocation;
    if(!regionManager) regionManager = [[CLLocationManager alloc] init];
    [regionManager setDelegate:self];

    CLLocationCoordinate2D plug2D;
    plug2D.latitude=plugLocation.coordinate.latitude;
    plug2D.longitude=plugLocation.coordinate.longitude;
    CLRegion *plugHome = [[CLRegion alloc] 
                            initCircularRegionWithCenter:plug2D 
                            radius:20 
                            identifier:@"Your Plug"];
    [regionManager startMonitoringForRegion:plugHome desiredAccuracy:kCLLocationAccuracyBest];
}
}

debug output:
2012-05-25 21:28:29.921 TestLocation[4412:707]  locationManager didUpdateToLocation
2012-05-25 21:28:29.929 TestLocation[4412:707] Distance from plug=0.000000
2012-05-25 21:28:30.700 TestLocation[4412:707]  locationManager didUpdateToLocation
2012-05-25 21:28:30.704 TestLocation[4412:707] Distance from plug=9.897339
2012-05-25 21:28:33.106 TestLocation[4412:707] Distance from plug=15.923317
2012-05-25 21:28:34.783 TestLocation[4412:707] region monitoring started
(identifier Your Plug) <+47.61713686,-122.28694184> radius 20.00m
2012-05-25 21:28:34.784 TestLocation[4412:707] region monitoring started
(identifier Your Plug) <+47.61713686,-122.28694184> radius 20.00m
2012-05-25 21:28:34.788 TestLocation[4412:707] Distance from plug=19.705339
2012-05-25 21:28:56.894 TestLocation[4412:707] Distance from plug=12.808387
2012-05-25 21:28:57.894 TestLocation[4412:707] Distance from plug=12.808387
2012-05-25 21:29:05.893 TestLocation[4412:707] Distance from plug=12.808387
2012-05-25 21:29:19.890 TestLocation[4412:707] Distance from plug=12.808387
2012-05-25 21:30:02.892 TestLocation[4412:707] Distance from plug=14.614380
2012-05-25 21:30:32.893 TestLocation[4412:707] Distance from plug=14.614380
2012-05-25 21:30:52.891 TestLocation[4412:707] Distance from plug=15.352976
2012-05-25 21:32:41.898 TestLocation[4412:707] Distance from plug=7.644082
2012-05-25 21:32:43.891 TestLocation[4412:707] Distance from plug=12.354503
2012-05-25 21:32:49.068 TestLocation[4412:707] Distance from plug=15.757565
2012-05-25 21:32:52.984 TestLocation[4412:707] Distance from plug=21.126129
2012-05-25 21:32:56.880 TestLocation[4412:707] Distance from plug=26.952410
2012-05-25 21:33:04.925 TestLocation[4412:707] Distance from plug=31.824801
4

1 回答 1

0

您是否实施了正确的区域监控回调方法?

-didEnterRegion
-didExitRegion

关于区域监控的一些信息。它并不像你想象的那么精确。将添加一个最小区域大小。最小大小为 100M。因此,您在 100M 以下添加的任何内容都会增加到 100M。

看起来您正在输出中获取位置更新,所以我猜您只需要实现 CLLocationManagerDelegate 方法来处理区域监控。您还可以监控以确保正确添加区域。那里可能是一个错误,导致没有回调。

-monitoringDidFailForRegion

希望这可以帮助。

于 2012-07-09T18:44:00.553 回答