首先創(chuàng)建locationManager ,這樣就可以驗(yàn)證地理位置服務(wù)驗(yàn)證。
self.locationManager= [[CLLocationManageralloc]init];
locationManager 有個(gè)代理方法 CLLocationManagerDelegate
self.locationManager.delegate=self;
//超定一定范圍調(diào)用
self.locationManager.distanceFilter=kCLLocationAccuracyHundredMeters;
kCLLocationAccuracyHundredMeters 適合徒步。
// 定位精度
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
//? 程序進(jìn)入后臺(tái)? significantLocationChangeMonitoringAvailable? 判斷設(shè)備是否支持重要位置變化的監(jiān)測(cè)
[[NSNotificationCenterdefaultCenter]addObserverForName:UIApplicationWillResignActiveNotificationobject:nilqueue:nilusingBlock:^(NSNotification*_Nonnullnote) {
// Stop normal location updates and start significant location change updates for battery efficiency.?
if([CLLocationManager significantLocationChangeMonitoringAvailable]) {
[weakSelf.locationManagerstopUpdatingLocation];
[weakSelf.locationManagerstartMonitoringSignificantLocationChanges];
}else{
NSLog(@"Significant location change monitoring is not available.");
}
}];
// ?程序進(jìn)入foreground
[[NSNotificationCenterdefaultCenter]addObserverForName:UIApplicationWillEnterForegroundNotificationobject:nilqueue:nilusingBlock:^(NSNotification*_Nonnullnote) {
if([CLLocationManagersignificantLocationChangeMonitoringAvailable]) {
// Stop significant location updates and start normal location updates again since the app is in the forefront.
[weakSelf.locationManagerstartUpdatingLocation];
[weakSelf.locationManagerstopMonitoringSignificantLocationChanges];
}
else{
NSLog(@"Significant location change monitoring is not available.");
}
}];
用到三個(gè)方法分別是
// 驗(yàn)證發(fā)生變化得時(shí)候開始定位。
- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if(status ==kCLAuthorizationStatusAuthorizedAlways|| status ==kCLAuthorizationStatusAuthorizedWhenInUse) {
[self.locationManagerstartUpdatingLocation];
}
}
// 地理位置發(fā)生改變時(shí)觸發(fā)? 通過(guò)CLGeocoder 進(jìn)行地理編碼 獲取地域位置。
- (void)locationManager:(CLLocationManager*)manager
didUpdateLocations:(NSArray*)locations {
CLLocation*currLocation = [locationslastObject];
NSLog(@"經(jīng)度=%f 緯度=%f 高度=%f", currLocation.coordinate.latitude,
currLocation.coordinate.longitude, currLocation.altitude);
m_pLocationCoord= currLocation.coordinate;
CLLocation*c =
[[CLLocationalloc]initWithLatitude:currLocation.coordinate.latitude
longitude:currLocation.coordinate.longitude];
//創(chuàng)建位置
CLGeocoder*revGeo = [[CLGeocoderalloc]init];
[revGeoreverseGeocodeLocation:c
completionHandler:^(NSArray*placemarks,NSError*error) {
if(!error && [placemarkscount] >0) {
NSDictionary*dict1 =
[[placemarksobjectAtIndex:0]addressDictionary];
NSLog(@"%@", dict1);
NSString*stringCity = [dict1objectForKey:@"City"];
NSArray*array =
[stringCitycomponentsSeparatedByString:@"市"];
if(array.count>0) {
[[NSNotificationCenterdefaultCenter]postNotificationName:kNotificationLocationCitySucceedobject:[arrayobjectAtIndex:0]];
}
}else{
//GetLoctionCity(@"定位失敗");
NSLog(@"ERROR: %@", error);
}
}];
}
// 定位失敗
- (void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error {
//NSLog(@"didFailWithError: %@", error);
if([errorcode] ==kCLErrorDenied) {
//訪問(wèn)被拒絕
[[NSNotificationCenterdefaultCenter]postNotificationName:kNotificationLocationCityFailobject:@"請(qǐng)打開該app的位置服務(wù)!"];
}
if([errorcode] ==kCLErrorLocationUnknown) {
//無(wú)法獲取位置信息
[[NSNotificationCenterdefaultCenter]postNotificationName:kNotificationLocationCityFailobject:@"定位失??!"];
}
}
在 不用定位的時(shí)候 stopUpdatingLocation?