我最近解决了这个问题,并最终通过巧妙地阅读文档发现 CoreLocation 在单独的线程中运行,因此您可以启动它,然后在它更新时检索事件。它位于“获取用户位置”标题下的文档中。所以这里是你开始更新的地方:
- (void)startStandardUpdates
{
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = none;
[locationManager startUpdatingLocation];
}
如果您将委托设置为“self”,它将向定义 start 方法的同一类发送事件,因此您只需添加以下内容即可检索事件:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if (newLocation.horizontalAccuracy < 30.0)
{
NSLog(@"latitude %+.6f, longitude %+.6f\n",
newLocation.coordinate.latitude,
newLocation.coordinate.longitude);
[manager stopUpdatingLocation];
}
}
这样它将继续接收事件,然后关闭 GPS 接收器以节省能源。当然,如果超时,它需要一个超时时间和某种方式来存储和接受具有最佳水平精度的位置,但我还没有弄清楚如何做到这一点。