在項目功效中有一個定位CLLocation的需求,碰到了一些常識難點,經由列位年夜俠的贊助,成績處理,特此分享供年夜家進修,願望年夜家配合進修提高。
1、簡略解釋
1.CLLocationManager
CLLocationManager的經常使用操作和屬性
開端用戶定位- (void)startUpdatingLocation;
停滯用戶定位- (void) stopUpdatingLocation;
解釋:當挪用了startUpdatingLocation辦法後,就開端赓續地定位用戶的地位,半途會頻仍地挪用署理的上面辦法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
每隔若干米定位一次
@property(assign, nonatomic) CLLocationDistance distanceFilter;
定位准確度(越准確就越耗電)
@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;
應用定位功效,起首要導入框架,遵照CLLocationManagerDelegate協定,再創立地位治理器CLLocationManager
在IOS8.0後,定位功效須要在info.plist中參加NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription這兩個NSString類型字段,能力夠應用定位功效
代碼貼出來與年夜家共勉,列位看官自行研討
{ self.locationManager = [[CLLocationManager alloc] init]; _locationManager.delegate = self; if([CLLocationManager locationServicesEnabled] == NO) { // NSLog(@"沒有GPS辦事"); } //地輿地位准確度 _locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters; //設置間隔挑選器,double類型,只需間隔變更若干,就挪用拜托署理 self.locationManager.distanceFilter = kCLDistanceFilterNone; // meters [_locationManager requestWhenInUseAuthorization];// 前台定位 [_locationManager startUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { NSLog(@"longitude = %f", ((CLLocation *)[locations lastObject]).coordinate.longitude); NSLog(@"latitude = %f", ((CLLocation *)[locations lastObject]).coordinate.latitude); CGFloat longTI=((CLLocation *)[locations lastObject]).coordinate.longitude; CGFloat latTI=((CLLocation *)[locations lastObject]).coordinate.latitude; //將經度顯示到label上 _longitudeLabel.text = [NSString stringWithFormat:@"%f",longTI]; //將緯度實際到label上 _latitudeLabel.text = [NSString stringWithFormat:@"%f",latTI]; // 獲得以後地點的城市名 CLGeocoder *geocoder = [[CLGeocoder alloc] init]; //依據經緯度反向地輿編譯出地址信息 [geocoder reverseGeocodeLocation:locations.lastObject completionHandler:^(NSArray *array, NSError *error) { if (array.count > 0) { CLPlacemark *placemark = [array objectAtIndex:0]; // //將取得的一切信息顯示到label上 // self.location.text = placemark.name; //獲得城市 NSString *city = placemark.locality; if (!city) { //四年夜直轄市的城市信息沒法經由過程locality取得,只能經由過程獲得省分的辦法來取得(假如city為空,則可知為直轄市) city = placemark.administrativeArea; } // NSLog(@"city = %@", city); _cityName=city; } else if (error == nil && [array count] == 0) { // NSLog(@"No results were returned."); } else if (error != nil) { // NSLog(@"An error occurred = %@", error); } }]; //體系會一向更新數據,直到選擇停滯更新,由於我們只須要取得一次經緯度便可,所以獲得以後就停滯更新 [manager stopUpdatingLocation]; }
以上是關於本站小編給年夜家整頓的IOS開辟之詳解定位CLLocation,後續還會連續更新,願望年夜家可以或許愛好。
【IOS開辟筆記整頓49之詳解定位CLLocation】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!