我正在创建一个应用程序,告诉用户他们是否靠近目的地。
我正在计算之间的距离currentLocation
和目的地。我正在里面做计算didUpdateLocations
。它正在工作,但我已经看到有一些方法可以处理这个问题,而无需进行任何数学计算。
我正在注册该区域CLLocationManager
;然而这些方法似乎didExitRegion
and 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];
}
}
];
}
我在这里做错了什么吗?
好的,在 iOS 中使用区域监控功能时需要记住一些事项。
- 无论您将初始半径设置为多少,区域都将默认为最小尺寸。一位 Apple 工程师告诉我,对于支持 GPS 的设备来说,这个值是 100M。 450M,适用于支持区域监控的仅 Wifi 设备(iPad 3 和新的 iPod Touch)
- 您可以监控的区域是有限的。单个设备上可以监控的总数是有限的。一位 Apple 工程师再次告诉我,大约有 100 个地区。使用委托方法来确保您的区域添加得好还是坏。
- 区域非常有用,并且对电池寿命的影响很小。他们还在状态栏中获得自己的位置图标。 (紫色空心位置箭头)
- 它们的工作方式与其他位置 API 非常相似,您需要正确响应委托方法以解释正在发生的操作。
您的代码看起来不错,但缺少围绕您的逻辑CLLocationManagerDelegate
。如果没有适当的委托来处理回调,您可能会错过回调(-didExitRegion/-didEnterRegion
).
根据我的经验,我创建一个单例类来处理所有位置管理器委托方法。请务必注册以收听他们的声音。如果您包含更多有关这些委托调用的代码,我很乐意为您提供更多帮助。有很多教程应该记录如何正确设置它们。祝你好运。
*注:我今年在 WWDC 上与一位位置工程师讨论了围绕最小区域大小和区域数量的许多未知因素。我可以确认最小区域大小为 100,但不能确认最大区域数。我目前还没有这个需求。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)