Geofencing iOS 6

Cam*_*mus 3 core-location cllocationmanager ios clregion region-monitoring

我正在创建一个应用程序,告诉用户他们是否在目的地附近.我正在计算currentLocation目的地之间的距离.我在里面做计算didUpdateLocations.它正在工作但我已经看到有一些方法可以解决这个问题,而无需进行任何数学运算.

我正在注册该地区CLLocationManager; 但似乎这些方法didExitRegiondidEnterRegion没有被调用.

以下是我注册区域的代码部分:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.locationManager startUpdatingLocation];
    [self.mySearchBar resignFirstResponder];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

    self.distToRemind = 0;

    [worldMap removeAnnotations:[worldMap annotations]];
    NSLog(@"executou de primeira");

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:[self.mySearchBar text] completionHandler:^(NSArray *placemarks, NSError *error)
         {
             CLPlacemark *placemark = [placemarks lastObject];

             //test
             //DefaultAnnotation *annot = [[DefaultAnnotation alloc] initWithCoordinate:placemark.location.coordinate andTitle:@""];
             CLRegion *newRegion = [[CLRegion alloc] initCircularRegionWithCenter:placemark.location.coordinate radius:10.0 identifier:@"RegionBoundary"];

             DefaultAnnotation *regionAnnotation = [[DefaultAnnotation alloc] initWithCoordinate:newRegion.center andTitle:@""];

             [self identifyPlacemark:placemark andSetAnnotation:regionAnnotation];

             MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(regionAnnotation.coordinate, 250, 250);


             [worldMap addAnnotation:regionAnnotation];
             [worldMap setRegion:region animated:YES];

             [self.locationManager startMonitoringForRegion:newRegion];

             if (self.boolPushButtonTapped) {
                  [self pushButtonTapped];
             }
         }
         ];
}
Run Code Online (Sandbox Code Playgroud)

我在这里做错了吗?

Bil*_*ess 10

好的,在iOS中使用区域监控功能时要记住一些事项.

  • 无论您将初始半径设置为何,区域都将默认为最小大小.一位Apple工程师告诉我,支持GPS的设备是100M.450M仅支持Wifi设备,支持区域监控(iPad 3和新iPod Touch)
  • 您可以监控的区域是有限的商品.可以在单个设备上监控的总数是有限的.同样,一位Apple工程师告诉我它大约有100个地区.使用委托方法确保您的区域添加好或坏.
  • 区域非常有用,对电池寿命的影响最小.他们还在状态栏中获得自己的位置图标.(中空紫色位置箭头)
  • 它们与所有其他位置API非常相似,您需要正确响应委托方法以解释正在发生的操作.

你的代码看起来不错,但却缺少你的代码CLLocationManagerDelegate.没有适当的委托来处理回调,你可能只是错过了回调(-didExitRegion/-didEnterRegion).

根据我的经验,我创建了一个单例类来处理我的所有位置管理器委托方法.请务必注册以收听他们.如果你包含一些围绕这些委托电话的代码,我很乐意为你提供更多帮助.有很多教程应该记录如何正确设置它们.祝好运.

*注意:今年我在WWDC与位置工程师谈到了围绕最小区域大小和区域数量的许多未知因素.我可以确认最小区域大小为100,但不是最大区域数.到目前为止我还没有必要.