发现之前的地图获取当前地理位置信息在Deprecated in iOS 5.0。已经被苹果弃之不用了。推荐
使用CLGeocoder来替代。发现非常简单,比之前写的方法简单了不少。地图的前提是你导入了MapKit这个库
#import <MapKit/MKMapView.h>先声明一个全局的CLLocationManager对象。
CLLocationManager *_currentLoaction;之后开启定位功能。
_currentLoaction = [[CLLocationManager alloc] init];
_currentLoaction.delegate = self;
[_currentLoaction startUpdatingLocation];
定位结束之后更新当前的地址经纬度等信息。
#pragma mark - Location
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"locError:%@", error);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[_currentLoaction stopUpdatingLocation];
NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];
NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];
NSLog(@"Lat: %@ Lng: %@", strLat, strLng);
[_geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
_placeMark = [placemarks objectAtIndex:0];
_locationLabel.text = _placeMark.administrativeArea;
ITTDINFO(@"%@",_locationLabel.text);
// we have received our current location, so enable the "Get Current Address" button
}];
}解释_geocoder 这个是我先要声明的CLGeocoder。使用之前要alloc,才能使用。
我刚开始犯了一个低级错误,没有在viewDidLoad方法中_geocoder = [[CLGeocoder alloc] init];导致一直nil无法出现block的方法。
使用CLGeocoder替代DeprecatediOS5.0地图获取地理位置信息
本文介绍了在iOS5.0版本中地图获取地理位置信息已被弃用后,如何使用CLGeocoder进行替代。通过声明全局CLLocationManager对象、开启定位功能并更新地址经纬度信息,实现简单的地理位置获取流程。使用CLGeocoder前需注意初始化对象,避免因未初始化导致的nil问题。
1万+

被折叠的 条评论
为什么被折叠?



