地理围栏无法正常运行

时间:2014-05-20 05:46:59

标签: ios cllocationmanager geofencing

在我的应用程序中,我想做地理围栏。我有位置列表,我正在设置地理围栏区域。 所有位置都有半径100米。以下是设置Region的代码:

CLLocationCoordinate2D cord;
cord.latitude = [location.latitude doubleValue];
cord.longitude = [location.longtitude doubleValue];
CLLocationDistance regionRadius = location.radius;
CLRegion *region  = [[CLRegion alloc] initCircularRegionWithCenter:cord radius:regionRadius identifier:[NSString stringWithFormat:@"%d",location.locId]];
[appDelegate.locationManager startMonitoringForRegion:region];

我的问题是,它无法正常工作。对于调试,在didEnterRegion委托中增加了当前位置和区域位置之间的距离计算,有时这个距离大于 1500m

以下是计算当前位置和区域位置之间距离的代码:

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    CLLocation *loc1 = locationManager.location;
    CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:region.center.latitude longitude:region.center.longitude];
    double dist = [loc1 distanceFromLocation:loc2];
}

任何想法,为什么-locationManager:didEnterRegion:会被触发这么错误的方式?

1 个答案:

答案 0 :(得分:3)

这是 iOS Region Monitoring 中的一个小故障,我遇到了与 Geofencing 类似的问题,我找到了解决该问题的方法。

我在这里发布了这个问题并且我已经回答了自己,我也有一篇博文: -

  1. Region Monitoring Glitch on iOS 7 - Multiple Notifications at the same time
  2. iOS Region Monitoring and Location Manager