iOS 定位于地理反编码

时间:2021-11-06 21:57:54
- (void)viewDidLoad {

[self startLocation];
} //开始定位
-(void)startLocation{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self; [self.locationManager requestWhenInUseAuthorization];
if ([[[UIDevice currentDevice]systemVersion]doubleValue]>=8.0) { [self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization]; }
[self.locationManager startUpdatingLocation]; if ([CLLocationManager locationServicesEnabled]) {
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
}else
{
NSLog(@"open fail");
} }
#pragma mark - CoreLocation 代理
#pragma mark 跟踪定位代理方法,每次位置发生变化即会执行(只要定位到相应位置)
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location=[locations lastObject];
CLLocationCoordinate2D coordinate=location.coordinate;
NSLog(@"经度:%f,纬度:%f",coordinate.longitude,coordinate.latitude);
self.longitude=coordinate.longitude;
self.latitude=coordinate.latitude;
//如果不需要实时定位,使用完即使关闭定位服务
[self.locationManager stopUpdatingLocation];
[self weiZhi];
}
-(void)weiZhi
{
self.geocoder=[[CLGeocoder alloc]init];
CLLocation *rr=[[CLLocation alloc]initWithLatitude:self.latitude longitude:self.longitude];
[self.geocoder reverseGeocodeLocation:rr completionHandler:^(NSArray *placemarks, NSError *error) { CLPlacemark *mark=[placemarks objectAtIndex:];
UILabel *currentLocation=[[UILabel alloc]initWithFrame:CGRectMake(, , , )];
currentLocation.text=[NSString stringWithFormat:@"%@",mark.subLocality]; [self.locationView addSubview:currentLocation];
}]; }
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}
break;
default:
break;
} }
//当定位出现错误时就会调用这个方法。
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"error-%@",error); }

定位当前的位置。对于 CLPlacemark 还有许多属性,不仅仅可以给出当前位置,还可以给出其他信息,后续研究。。。。