CoreLocation框架

首先創(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?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一. iOS8.0之前的定位(?????) 1. 前臺(tái)定位 導(dǎo)入CoreLocation框架以及對(duì)應(yīng)的主頭文件 #...
    走道牙的人閱讀 281評(píng)論 0 0
  • CoreLocation框架 一. iOS8.0之前的定位(?????) 1. 前臺(tái)定位 導(dǎo)入CoreLocati...
    0271fb6f797c閱讀 585評(píng)論 0 1
  • CoreLocation框架 一. iOS8.0之前的定位 1. 前臺(tái)定位 導(dǎo)入CoreLocation框架以及對(duì)...
    iOS_Cqlee閱讀 1,420評(píng)論 1 2
  • CoreLocation框架 一. iOS8.0之前的定位(?????) 1. 前臺(tái)定位 導(dǎo)入CoreLocati...
    Jack__yang閱讀 237評(píng)論 0 4
  • 全書目錄:碧血桃花(全) 上回:北上雁門 第十六回 謀雁門 1 青龍二十三年,五月初八。 蕭維點(diǎn)起五千常備軍,朝著...
    西行寺菜菜子閱讀 263評(píng)論 0 3

友情鏈接更多精彩內(nèi)容