CLLocationManager didUpdateLocations 或 didUpdateToLocation
CLLocationManager didUpdateLocations or didUpdateToLocation
我 iOS 我正在使用 CLLocationManager
,但我不确定使用哪种委托方法来获取位置更新。有两种方法
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
和
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
我知道我应该使用第二个,因为第一个已弃用,但我已将我的应用程序的部署目标设置为 6.0。那么我应该使用哪一个?在附图 中,就在这个方法旁边
(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
它说 6.0
。那么它在 iOS 6.0
中被弃用或在 6.0 之前可用是什么意思。我的猜测是,我应该使用
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
应该没问题。有什么建议么?
我以前的项目也遇到了同样的问题,所以我尝试了下面的方法,然后对我来说很好用
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
上面的委托在 iOS6 中被弃用。现在应该使用以下内容:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
为了得到最后的位置,只需得到数组的最后一个对象:
[locations lastObject]
换句话说,[locations lastObject]
(新代表)等于newLocation
(旧代表)
示例
iOS6岁以上
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *newLocation = [locations lastObject];
CLLocation *oldLocation;
if (locations.count >= 2) {
oldLocation = [locations objectAtIndex:locations.count-1];
} else {
oldLocation = nil;
}
NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);
}
如果在 iOS6 及以下使用,还要添加以下方法
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[self locationManager:locationManager didUpdateLocations:[[NSArray alloc] initWithObjects:newLocation, nil]];
}
如需更多参考,请遵循此 tutorial
我 iOS 我正在使用 CLLocationManager
,但我不确定使用哪种委托方法来获取位置更新。有两种方法
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
和
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
我知道我应该使用第二个,因为第一个已弃用,但我已将我的应用程序的部署目标设置为 6.0。那么我应该使用哪一个?在附图
(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
它说 6.0
。那么它在 iOS 6.0
中被弃用或在 6.0 之前可用是什么意思。我的猜测是,我应该使用
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
应该没问题。有什么建议么?
我以前的项目也遇到了同样的问题,所以我尝试了下面的方法,然后对我来说很好用
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
上面的委托在 iOS6 中被弃用。现在应该使用以下内容:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
为了得到最后的位置,只需得到数组的最后一个对象:
[locations lastObject]
换句话说,[locations lastObject]
(新代表)等于newLocation
(旧代表)
示例
iOS6岁以上
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *newLocation = [locations lastObject];
CLLocation *oldLocation;
if (locations.count >= 2) {
oldLocation = [locations objectAtIndex:locations.count-1];
} else {
oldLocation = nil;
}
NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);
}
如果在 iOS6 及以下使用,还要添加以下方法
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[self locationManager:locationManager didUpdateLocations:[[NSArray alloc] initWithObjects:newLocation, nil]];
}
如需更多参考,请遵循此 tutorial