CoreLocation框架

首先创建locationManager ,这样就可以验证地理位置服务验证。

self.locationManager= [[CLLocationManageralloc]init];

locationManager 有个代理方法 CLLocationManagerDelegate

self.locationManager.delegate=self;

//超定一定范围调用

self.locationManager.distanceFilter=kCLLocationAccuracyHundredMeters;

kCLLocationAccuracyHundredMeters 适合徒步。

// 定位精度

self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;


//  程序进入后台  significantLocationChangeMonitoringAvailable  判断设备是否支持重要位置变化的监测

[[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.");

}

}];

//  程序进入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.");

}

}];




用到三个方法分别是

// 验证发生变化得时候开始定位。

- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{

if(status ==kCLAuthorizationStatusAuthorizedAlways|| status ==kCLAuthorizationStatusAuthorizedWhenInUse) {

[self.locationManagerstartUpdatingLocation];

}

}

// 地理位置发生改变时触发  通过CLGeocoder 进行地理编码 获取地域位置。

- (void)locationManager:(CLLocationManager*)manager

didUpdateLocations:(NSArray*)locations {

CLLocation*currLocation = [locationslastObject];

NSLog(@"经度=%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];

//创建位置

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) {

//访问被拒绝

[[NSNotificationCenterdefaultCenter]postNotificationName:kNotificationLocationCityFailobject:@"请打开该app的位置服务!"];

}

if([errorcode] ==kCLErrorLocationUnknown) {

//无法获取位置信息

[[NSNotificationCenterdefaultCenter]postNotificationName:kNotificationLocationCityFailobject:@"定位失败!"];

}

}

在 不用定位的时候 stopUpdatingLocation 

你可能感兴趣的:(CoreLocation框架)