最近做项目时用到地图,于是自己就封装了一个类,关于地图的一些常用方法,编码,反编码,定位之类的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | // // 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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | // // 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 |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· 程序员常用高效实用工具推荐,办公效率提升利器!
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)