先说一下它的神奇之处:
1.类的本身是一个单例类,但是每次调用属性,都能在此启动实时定位获取到当前位置数据,这样就实现了可以重用公共类的目的了。
2.命名:方法初始化一般都用setup这边也不例外
- (void)setup {
if ([CLLocationManager locationServicesEnabled]) {
_currentLocation = [[CLLocationManager alloc] init];
_currentLocation.delegate = self;
_currentLocation.distanceFilter = 200
_currentLocation.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
if (SYSTEM_VERSION >= 8.0) {
[_currentLocation requestAlwaysAuthorization];
}
[_currentLocation startUpdatingLocation];
}
}
3.每次用单例类调用这个block属性都会重新定位,同时获取到新的数据,妙!!!
- (void)getCurrentGeolocationsCompled:(GetLocationCompledBlock)compled{
self.getLocationCompledBlock = compled;
[_currentLocation startUpdatingLocation];
}
为了定位类不出bug注意点:
1.plist设置两个属性
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
2._geoCoder = [[CLGeocoder alloc] init]; 请设成全局对象
[_geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {