IOS開發筆記整理49之詳解定位CLLocation_IOS

來源:互聯網
上載者:User

在項目功能中有一個定位CLLocation的需求,遇到了一些知識痛點,經過各位大俠的協助,問題解決,特此分享供大家學習,希望大家共同學習進步。

一、簡單說明

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,後續還會持續更新,希望大家能夠喜歡。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.