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


//
//  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

优质内容筛选与推荐>>
1、转载-隐藏在手机中的第二个操作系统
2、c++ 设计模式
3、My interested stuff(2008-08-03)
4、学习进度
5、HOOKAPI之修改IAT法则


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn