我正在创建一个应用程序,告诉用户他们是否在目的地附近。我正在计算currentLocation和目的地之间的距离。我在didUpdateLocations中进行计算。它是有效的,但我已经看到有一些方法可以在不需要做任何数学计算的情况下处理它。
我在CLLocationManager中注册了该区域;但是,didExitRegion和didEnterRegion方法似乎没有被调用。
下面是我注册区域的代码部分:
- (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];
}
}
];
}我是不是做错了什么?
发布于 2012-10-05 05:25:26
好的,在使用iOS中的区域监控功能时,需要记住几件事。
无论将初始半径设置为什么值,
非常相似,您需要正确响应委托方法才能解释正在发生的操作。
您的代码看起来不错,但缺少围绕CLLocationManagerDelegate的逻辑。如果没有合适的委托来处理回调,您很可能会错过回调( -didExitRegion/-didEnterRegion)。
根据我的经验,我创建了一个单例类来处理我所有的位置管理器委托方法。一定要注册来倾听他们的声音。如果您在这些委托调用中包含更多代码,我将很乐意为您提供更多帮助。有大量的教程应该记录如何正确地设置它们。祝好运。
*注:今年我与WWDC的一位位置工程师谈到了许多关于最小区域大小和区域数量的未知因素。我可以确认最小区域大小为100,但不能确认最大区域数。到目前为止,我还没有这个需要。
https://stackoverflow.com/questions/12720658
复制相似问题