最近做项目时用到地图,于是自己就封装了一个类,关于地图的一些常用方法,编码,反编码,定位之类的

//
//  GetLocation.h
//  智慧城
//
//  Created by mac on 15/12/25.
//  Copyright (c) 2015年 ZY. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface GetLocation : NSObject<CLLocationManagerDelegate>

@property (nonatomic,strong)CLLocation *location;

@property (nonatomic,strong)CLGeocoder *geocoder;

@property (nonatomic,strong)CLLocationManager *manager;

@property (nonatomic,copy)void(^LBlock)(NSString *cityName);

+ (instancetype)shareInstanceType;

- (void)getCurrentLocation;


@end

 

//
//  GetLocation.m
//  智慧城
//
//  Created by mac on 15/12/25.
//  Copyright (c) 2015年 ZY. All rights reserved.
//

#import "GetLocation.h"

@implementation GetLocation

+ (instancetype)shareInstanceType{
    
    static GetLocation *getLo = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        getLo = [[[self class] alloc] init];
    });
    
    return getLo;
}

- (CLLocationManager *)manager{
    if (!_manager) {
        
        _manager = [[CLLocationManager alloc] init];
        
        _manager.delegate = self;
        
    }
    
    
    return _manager;
}

- (CLLocation *)location{
    
    if (!_location) {
        
        _location = [[CLLocation alloc] init];
    }
    
    return _location;
}

- (CLGeocoder *)geocoder{
    
    if (!_geocoder) {
        
        _geocoder = [[CLGeocoder alloc] init];
    }
    
    return _geocoder;
}



#pragma mark 对当前位置进行定位
- (void)getCurrentLocation{
    
    //判断手机是否开启定位功能
    BOOL isOpen = [CLLocationManager locationServicesEnabled];
    
    if (isOpen) {
        
        NSLog(@"定位开启");
        
    }else{
        
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"未开启定位" message:@"请打开手机定位" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alert show];
        return;
    }
    
    //获取定位服务的授权状态
    NSInteger status = [CLLocationManager authorizationStatus];
    
    if (status == 0) {
        //判断版本
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
            
            //获取硬件定位授权
            //应用使用时定位
            //            [self.manager requestAlwaysAuthorization];
            //一直可以定位
            [self.manager requestWhenInUseAuthorization];
            
            //            info.plist文件设置
            //NSLocationAlwaysUsageDescription---我想在后台还访问您的位置
            //NSLocationWhenInUseUsageDescription---我想在我的app开启的时候使用您的位置,可以吗?
            
        }
        
    }else if (status == 2){
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"手机未对本应用开启定位限制,请开启" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alert show];
        return;
        
    }
    
    //    定位功能属性
    //设置定位精度
    self.manager.desiredAccuracy = kCLLocationAccuracyBest;
    //设置定位频率
    self.manager.distanceFilter = 100.f;
    //开启定位
    [self.manager startUpdatingLocation];


    
    
}

#pragma mark 反编码
- (void)getCityNameUseLatitude:(double)latitude withLongitude:(double)longitude{
    
    
    _location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    
    [self.geocoder reverseGeocodeLocation:_location completionHandler:^(NSArray *placemarks, NSError *error) {
        
        if (error) {
            
            NSLog(@"反编码失败==%@",error);
            
        }else{
            
            //获取反编码的坐标
            CLPlacemark *pm = [placemarks firstObject];
            
            // country:国家
            //administrativeArea:省份
            //locality:城市
            //subLocality:区
            //thoroughfare:街道
            //subThoroughfare:编码
            
            
            NSLog(@"%@",[NSString stringWithFormat:@"%@-%@-%@-%@-%@-%@",pm.country,pm.administrativeArea,pm.location,pm.subLocality,pm.thoroughfare,pm.subThoroughfare]);
            
            NSLog(@"地区个数:%ld",(unsigned long)placemarks.count);
            
            for (CLPlacemark *place in placemarks) {
                
//                NSLog(@"======");
                
                [place.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
                    
                    if(_LBlock){
                        
                        _LBlock(obj);
                        
                    }
                    NSLog(@"%@:%@",key,obj);
                    
                }];
                
//                NSLog(@"=====");
                
            }
            
            
        }
        
    }];
    
}



#pragma mark 地理编码 输入地名
- (void)getCLLocationCoordinateWithCityName:(NSString *)cityName{
    
    [self.geocoder geocodeAddressString:cityName completionHandler:^(NSArray *placemarks, NSError *error) {
        
        if (error) {
            
            NSLog(@"你输入的地名不存在");
            
        }
        
        //获取地标
        CLPlacemark *pm = [placemarks firstObject];
        
        //获取地理坐标
        CLLocationCoordinate2D coor = pm.location.coordinate;
        
        NSLog(@"经度:%.2f \n 纬度:%.2f",coor.longitude,coor.latitude);
        
        NSLog(@"城市个数:%ld",(unsigned long)placemarks.count);
        
        for (CLPlacemark *place in placemarks) {
            
            [place.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
                
                NSLog(@"%@:%@",key,obj);
                
            }];
            
            
        }
        
    }];
    

}

#pragma mark CLLocationDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    
    CLLocation *loaction = [locations firstObject];
    
    //按CLLocationCoordinate2D:结构体获取定位的经纬度
    /*typedef struct {
     CLLocationDegrees latitude;
     CLLocationDegrees longitude;
     } CLLocationCoordinate2D;*/
    
    CLLocationCoordinate2D coor = loaction.coordinate;
    
    NSLog(@"%.2f    %.2f",coor.latitude,coor.longitude);
    
    [self getCityNameUseLatitude:coor.latitude withLongitude:coor.longitude];
    
}


- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    
    NSLog(@"---%d",status);
    
    switch (status) {
        case 0:
            
            NSLog(@"纬度应用授权做出判断");
            break;
            
        default:
            break;
    }
    
    
}

//开始检测某一个区域时调用的方法
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region{
    
    NSLog(@"%s",__FUNCTION__);
    
}

//进入某一个区域调用的方法
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
    
    NSLog(@"%s",__FUNCTION__);
    
}

//离开某一个区域调用的方法
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
    
    NSLog(@"%s",__FUNCTION__);
}



@end

 

你可能感兴趣的:(最近做项目时用到地图,于是自己就封装了一个类,关于地图的一些常用方法,编码,反编码,定位之类的)