先說一下它的神奇之處:
1.類的本身是一個單例類,但是每次調(diào)用屬性,都能在此啟動實(shí)時定位獲取到當(dāng)前位置數(shù)據(jù),這樣就實(shí)現(xiàn)了可以重用公共類的目的了。
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.每次用單例類調(diào)用這個block屬性都會重新定位,同時獲取到新的數(shù)據(jù),妙?。?!
- (void)getCurrentGeolocationsCompled:(GetLocationCompledBlock)compled{
? ? self.getLocationCompledBlock = compled;
? ? [_currentLocation startUpdatingLocation];
}
為了定位類不出bug注意點(diǎn):
1.plist設(shè)置兩個屬性
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
2._geoCoder = [[CLGeocoder alloc] init]; 請?jiān)O(shè)成全局對象
[_geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {