国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

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

2019-10-21 18:56:49
字體:
來源:轉載
供稿:網友
在項目功能中有一個定位CLLocation的需求,遇到了一些知識難點,經過各位大俠的幫助,問題解決,特此分享供大家學習,希望大家共同學習進步
 

在項目功能中有一個定位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,后續還會持續更新,希望大家能夠喜歡。



注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安义县| 丰台区| 定安县| 凤凰县| 北流市| 赤峰市| 体育| 辽宁省| 安图县| 黔南| 德兴市| 芮城县| 施甸县| 陈巴尔虎旗| 监利县| 施秉县| 济阳县| 于都县| 应城市| 长宁县| 通渭县| 六枝特区| 台北市| 仁布县| 石狮市| 彰武县| 察雅县| 九江市| 桓台县| 修武县| 岚皋县| 肥乡县| 崇义县| 满洲里市| 陆河县| 吉水县| 岑巩县| 抚州市| 承德市| 彩票| 临海市|