ios 简单获取地理位置信息


ZTCLLocationManager.h

#import <CoreLocation/CoreLocation.h>


@interface ZTCLLocationManager : CLLocationManager


+ (id)shareZTCLLocationManager;

/** 开始获取经纬度*/

- (void)startGetLocationManager;

/** 经纬度获取的地址数组(中文)*/

- (NSArray *)getLocationAddressNameArray;


@end


ZTCLLocationManager.m

#import "ZTCLLocationManager.h"


#define IOS8_OR_LATER  ( [[[UIDevice currentDevice] systemVersion] compare:@"8.0"] != NSOrderedAscending )

@interface ZTCLLocationManager()<CLLocationManagerDelegate>{

    NSMutableDictionary *_locationFinallDic;    //key:经度 valueL:纬度

}


@property (nonatomic, strong) NSMutableArray *locationNameArray;

@end


@implementation ZTCLLocationManager


+ (id)shareZTCLLocationManager{

    static ZTCLLocationManager *share = nil;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        share = [[self alloc]init];

    });

    return share;

}


- (void)startGetLocationManager{

    

    if (_locationFinallDic == nil) {

        _locationFinallDic = [[NSMutableDictionary alloc]init];

    }else{

        [_locationFinallDic removeAllObjects];

    }

    

    if (self.locationNameArray == nil) {

        self.locationNameArray = [[NSMutableArray alloc]init];

    }else{

        [self.locationNameArray removeAllObjects];

    }

    

    if(IOS8_OR_LATER){

        [self requestWhenInUseAuthorization];

    }

    

    [self setDesiredAccuracy:kCLLocationAccuracyBest];

    self.distanceFilter = 10;  // 指定最小更新的距离为10

    self.delegate = self;

    

    [self startUpdatingLocation];

}


- (NSArray *)getLocationAddressNameArray{

    NSArray *sendArr = [NSArray arrayWithArray:self.locationNameArray];

    for (int i = 0; i < self.locationNameArray.count; i ++) {

        NSLog(@"======= %@",[self.locationNameArray objectAtIndex:i]);

    }

    return sendArr;

}


#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{

    switch (status) {

        case kCLAuthorizationStatusNotDetermined:

            if ([self respondsToSelector:@selector(requestAlwaysAuthorization)]) {

                [self requestAlwaysAuthorization];

            }

            break;

        default:break;

    }

}


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    //该方法会运行多次 以最后的经纬度为准

    NSLog(@"开始定位......");

    CLLocation *location;

    if (locations.count > 0) {

        location = [locations objectAtIndex:0];

        NSLog(@"精度:%f 纬度:%f",location.coordinate.latitude,location.coordinate.longitude);

        

        static NSString *tempLocationFinalStr = nil;

        

        CLGeocoder *geocoder = [[CLGeocoder alloc]init];

        [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray*placemarks,NSError* error){

    

             if (error) {

                 //TODO: (CZ)需要处理无网络时的提示 获取位置信息异常

                 NSLog(@"reverseGeocodeLocation error: %@", error);

             }else{

                 //存储获取的名称的经纬度

                 CGFloat finalLatitude = 0;

                 CGFloat finalLongitude = 0;

                 

                 for (CLPlacemark* place in placemarks) {

                     NSLog(@"name %@",place.name); //位置

                     NSLog(@"thoroughfare %@",place.thoroughfare);//街道

                     //子街道

                     NSLog(@"subthoroughfare %@",place.subThoroughfare);

                     //

                     NSLog(@"loclitity %@",place.locality);

                     //

                     NSLog(@"subLocality %@",place.subLocality);

                     //国家

                     NSLog(@"country %@",place.country);

                     NSLog(@"----------------------");

                     

                     NSMutableString *loc = [NSMutableString new];

                     if (![place.locality isEqualToString:@"(null)"]) {

                         [loc appendString:[NSString stringWithFormat:@"%@",place.locality]];

                     }

                     if (![place.subLocality isEqualToString:@"(null)"]) {

                         [loc appendString:[NSString stringWithFormat:@"%@",place.subLocality]];

                     }

                     if (![place.thoroughfare isEqualToString:@"(null)"]) {

                         [loc appendString:[NSString stringWithFormat:@"%@",place.thoroughfare]];

                     }

                     if (![place.subThoroughfare isEqualToString:@"(null)"]) {

                         [loc appendString:[NSString stringWithFormat:@"%@",place.subThoroughfare]];

                     }

                     

                     //取最后一次的位置

                     if (loc.length > 0) {

                         finalLatitude = place.location.coordinate.latitude;

                         finalLongitude = place.location.coordinate.longitude;

                         tempLocationFinalStr = loc;

                     }

                 }

                 

                 //判断没有存储过 该经纬度 && 有位置

                 NSNumber *longitudeInDic = [_locationFinallDic objectForKey:[NSNumber numberWithDouble:finalLatitude]];

                 if ([longitudeInDic doubleValue] != finalLongitude && tempLocationFinalStr.length > 0) {

                     [_locationFinallDic setObject:[NSNumber numberWithDouble:finalLongitude] forKey:[NSNumber numberWithDouble:finalLatitude]];

                    [self.locationNameArray addObject:tempLocationFinalStr];

                 }

             }

         }];

        

        [manager stopUpdatingLocation];

    }

}


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{

    NSLog(@"didFailWithError :%@", error);

    switch (error.code) {

        case kCLErrorDenied:{

            //@"打开定位服务来允许我们确定您的位置"

        }

        case kCLErrorLocationUnknown:{

            //@"定位异常"

        }break;

        default:break;

    }

    [manager stopUpdatingLocation];

}


@end


使用

[[ZTCLLocationManager shareZTCLLocationManager]startGetLocationManager];

[[ZTCLLocationManager shareZTCLLocationManager]getLocationAddressNameArray];


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值