Geofencing iOS 6

我正在创build一个应用程序,告诉用户他们是否在目的地附近。 我正在计算currentLocation和目的地之间的距离。 我正在做didUpdateLocations里面的didUpdateLocations 。 它正在工作,但我已经看到,有一些方法可以处理,而不需要做任何的math计算。

我正在注册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]; } } ]; } 

我在这里做错了什么?

好的,在iOS中使用区域监控function时需要记住一些事项。

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

你的代码看起来不错,但是缺lessCLLocationManagerDelegate的逻辑。 没有一个适当的委托来处理callback,你可能只是缺lesscallback( -didExitRegion/-didEnterRegion )。

根据我的经验,我创build了一个单例类来处理我所有的位置pipe理器委托方法。 确保你注册听他们。 如果围绕这些代表呼叫包含更多代码,我很乐意为您提供更多帮助。 有很多教程应该logging如何正确设置它们。 祝你好运。

*注意:今年我与WWDC的一名位置工程师谈了很多关于最小区域大小和区域数量的未知数。 我可以确认最小区域大小为100,但不是最大区域数量。 我还没有这个需要。