CNLiveLocationManager.m
9.67 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//
// CNLiveLocationManager.m
// LocationManager
//
// Created by 梁星国 on 2018/9/19.
// Copyright © 2018年 cnlive. All rights reserved.
//
#import "CNLiveLocationManager.h"
@interface CNLiveLocationManager ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLGeocoder *geocoder;
@property (nonatomic, copy) LocationCoordinate2DBlock coordinate2DBlock;
@property (nonatomic, copy) Failure failure;
@end
@implementation CNLiveLocationManager
static CNLiveLocationManager *_manager = nil;
/// 单例初始化
+ (CNLiveLocationManager *)instance {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_manager = [[CNLiveLocationManager alloc] init];
});
return _manager;
}
/// 初始化
- (instancetype)init {
self = [super init];
if (self) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;//精度
_locationManager.distanceFilter = 10;
// 兼容iOS8.0版本
/* Info.plist里面加上2项中的一项
NSLocationAlwaysUsageDescription String YES
NSLocationWhenInUseUsageDescription String YES
*/
if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
// iOS8.0以上,使用应用程序期间允许访问位置数据
[_locationManager requestWhenInUseAuthorization];
// iOS8.0以上,始终允许访问位置信息
// [_locationManager requestAlwaysAuthorization];
}
_geocoder = [[CLGeocoder alloc] init];
}
return self;
}
/// 释放
+(void)attempDealloc {
_manager = nil;
}
#pragma mark - 定位方法
/// 定位回调实现
- (void)getLocation:(LocationCoordinate2DBlock)coordinate2DBlock failure:(Failure)failure {
if ([CLLocationManager locationServicesEnabled] == NO) {
return;
}
_coordinate2DBlock = coordinate2DBlock;
_failure = failure;
// 停止上一次定位
[_locationManager stopUpdatingLocation];
// 开始新一次定位
[_locationManager startUpdatingLocation];
}
/// 停止定位实现
- (void)stop {
[_locationManager stopUpdatingLocation];
}
/// 反向坐标获取地址信息实现
- (void)getLocationWithCoordinate2D:(CLLocationCoordinate2D)coordinate2D placemarkBlock:(CNLivePlacemarkBlock)placemarkBlock failure:(Failure)failure{
CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate2D.latitude longitude:coordinate2D.longitude];
[_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (!error) {
if (placemarks.count > 0) {
CLPlacemark *placemark = placemarks.lastObject;
CNLivePlacemark *livePlacemark = [CNLivePlacemark initWithCLPlacemark:placemark];
if (placemarkBlock) {
placemarkBlock(livePlacemark);
}
}else {
if (failure) {
failure(error);
}
}
}else{
if (failure) {
failure(error);
}
}
}];
}
/// 地理编码获取经纬度实现
- (void)getLocationWithAddress:(NSString *)address coordinate2DBlock:(LocationCoordinate2DBlock)coordinate2DBlock failure:(Failure)failure {
[_geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (!error) {
if (placemarks.count > 0) {
CLPlacemark *placemark = placemarks.lastObject;
if (coordinate2DBlock) {
coordinate2DBlock(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude);
}
}else {
if (failure) {
failure(error);
}
}
}else{
if (failure) {
failure(error);
}
}
}];
}
#pragma mark -
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
CLLocation *location = locations.lastObject;
CLLocationDegrees lat = location.coordinate.latitude;
CLLocationDegrees lng = location.coordinate.longitude;
NSLog(@"当前更新位置: 纬度: (%lf), 经度: (%lf)", lat, lng);
if (_coordinate2DBlock) {
_coordinate2DBlock(lat, lng);
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if (error.code == kCLErrorDenied) {
NSLog(@"访问被拒绝");
} else if (error.code == kCLErrorLocationUnknown) {
NSLog(@"无法获取位置信息");
}
if (_failure) {
_failure(error);
}
}
#pragma mark - 定位实例方法
/// 定位回调
+ (void)getLocation:(LocationCoordinate2DBlock)coordinate2DBlock failure:(Failure)failure{
[[CNLiveLocationManager instance] getLocation:coordinate2DBlock failure:failure];
}
/// 停止定位
+ (void)stop{
[[CNLiveLocationManager instance] stop];
}
/// 是否启用定位服务
+ (BOOL)locationServicesEnabled{
return [CLLocationManager locationServicesEnabled];
}
/// 反向地理编码获取地址信息
+ (void)getLocationWithLatitude:(NSString *)latitude longitude:(NSString *)longitude placemarkBlock:(CNLivePlacemarkBlock)placemarkBlock failure:(Failure)failure{
CLLocationCoordinate2D coordinate2D = CLLocationCoordinate2DMake([latitude doubleValue], [longitude doubleValue]);
[[CNLiveLocationManager instance] getLocationWithCoordinate2D:coordinate2D placemarkBlock:placemarkBlock failure:failure];
}
/// 反向地理编码获取地址信息
+ (void)getLocationWithCoordinate2D:(CLLocationCoordinate2D)coordinate2D PlacemarkBlock:(CNLivePlacemarkBlock)placemarkBlock failure:(Failure)failure{
[[CNLiveLocationManager instance] getLocationWithCoordinate2D:coordinate2D placemarkBlock:placemarkBlock failure:failure];
}
/// 地理编码获取经纬度
+ (void)getLocationWithAddress:(NSString *)address coordinate2DBlock:(LocationCoordinate2DBlock)coordinate2DBlock failure:(Failure)failure{
[[CNLiveLocationManager instance] getLocationWithAddress:address coordinate2DBlock:coordinate2DBlock failure:failure];
}
@end
@implementation CNLivePlacemark
- (id)mutableCopyWithZone:(NSZone *)zone {
CNLivePlacemark *copy = [[[self class] allocWithZone:zone] init];
_country = [self.country mutableCopy];
_province = [self.province mutableCopy];
_city = [self.city mutableCopy];
_county = [self.county mutableCopy];
_address = [self.address mutableCopy];
_placemarkId = self.placemarkId;
_type = [self.type mutableCopy];
return copy;
}
- (id)copyWithZone:(NSZone *)zone {
CNLivePlacemark *copy = [[[self class] allocWithZone:zone] init];
_country = [self.country copy];
_province = [self.province copy];
_city = [self.city copy];
_county = [self.county copy];
_address = [self.address copy];
_placemarkId = self.placemarkId;
_type = [self.type copy];
return copy;
}
- (NSString *)city {
NSString *cityName = _city;
if ([cityName hasSuffix:@"市辖区"]) {
cityName = [cityName substringToIndex:[cityName length] - 3];
}
if ([cityName hasSuffix:@"市"]) {
// cityName = [cityName substringToIndex:[cityName length]];
cityName = cityName;
}
if ([cityName isEqualToString:@"香港特別行政區"] || [cityName isEqualToString:@"香港特别行政区"]) {
cityName = @"香港";
}
if ([cityName isEqualToString:@"澳門特別行政區"] || [cityName isEqualToString:@"澳门特别行政区"]) {
cityName = @"澳门";
}
return cityName;
}
- (NSString *)province {
NSString *provinceName = _province;
if ([provinceName hasSuffix:@"省"]) {
provinceName = [provinceName substringToIndex:[provinceName length] - 1];
} else if ([provinceName hasSuffix:@"市"]) {
// provinceName = [provinceName substringToIndex:[provinceName length] - 1];
provinceName = provinceName;
}
return provinceName;
}
/// 实例化
+ (CNLivePlacemark *)initWithCLPlacemark:(CLPlacemark *)placemark {
CNLivePlacemark *mark = [[CNLivePlacemark alloc] init];
mark.country = placemark.country ? placemark.country : @"";
mark.province = placemark.administrativeArea ? placemark.administrativeArea : @"";
mark.city = placemark.locality ? placemark.locality : mark.province;
mark.county = placemark.subLocality ? placemark.subLocality : @"";
NSString *formatAddress = [NSString stringWithFormat:@"%@%@", placemark.thoroughfare ? placemark.thoroughfare : @"",
placemark.subThoroughfare ? placemark.subThoroughfare:@""];
mark.address = formatAddress;
return mark;
}
/// 取得省市
- (NSString *)getProvinceAndCity {
NSString *provinceName = _province ? : @"";
NSString *cityName = _city ? : @"";
if ([self.city isEqualToString:self.province]) {
provinceName = @"";
}
if ([cityName hasSuffix:@"市辖区"]) {
cityName = [cityName substringToIndex:[cityName length] - 3];
}
if ([cityName isEqualToString:@"香港特別行政區"] || [cityName isEqualToString:@"香港特别行政区"]) {
cityName = @"香港";
}
if ([cityName isEqualToString:@"澳門特別行政區"] || [cityName isEqualToString:@"澳门特别行政区"]) {
cityName = @"澳门";
}
return [provinceName stringByAppendingString:cityName];
}
@end