標籤:
定位:
引入標頭檔 #import <CoreLocation/CoreLocation.h>
聲明管理器屬性:@property(nonatomic,strong)CLLocationManager *manager;
第一步:初始化管理器
self.manager = [[CLLocationManager alloc] init];
第二步:進行隱私的判斷並授權
//進行隱私的判斷 if (![CLLocationManager locationServicesEnabled]) { NSLog(@"是否前往隱私進行設定,允許定位"); } //進行版本的判斷 if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 8.0) { //判斷授權狀態 if ([CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse) { //請求授權 [self.manager requestWhenInUseAuthorization]; } }
//在授權請求之前需要在inforplist中設定允許定位的內容:把NSLocationWhenInUseUsageDescription(NSLocationAlwaysUsageDescription這是另一種方式)輸入到inforplist中,然後在後邊輸入一句話;
第三步:設定管理員的代理和屬性
self.manager.delegate = self;
第四步:開啟定位
[self.manager startUpdatingLocation];
實現代理方法
//這個代理方法是定位成功之後開始更新位置資訊,只要移動的設定的最小距離之後也開始走這個方法;- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{ //擷取最後一次的位置 CLLocation *location = locations.lastObject; //擷取位置的座標 CLLocationCoordinate2D coordinate = location.coordinate; NSLog(@"經度:%f,緯度:%f,海拔:%f,航海方向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed); //為了節省電源,如果不使用定位,需要把定位關了 [self.manager stopUpdatingLocation];}//定位失敗- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ NSLog(@"定位失敗");}
編碼與反編碼
聲明編碼與反編碼類別:@property(nonatomic,strong)CLGeocoder *geocoder;
初始化對象:self.geocoder = [[CLGeocoder alloc] init];
根據地名擷取相關的資訊(編碼)
- (void)getCoordinateByAdress:(NSString *)address{ //編碼方法 [self.geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { //根據返回的地標,取出第一個(地標的位置很多) CLPlacemark *mark = placemarks.firstObject; //根據地標(mark)得到location CLLocation *location = mark.location; //根據mark擷取地區 CLRegion *region = mark.region; //擷取字典資訊 NSDictionary *addressDic = mark.addressDictionary; NSLog(@"地標位置:%@,地區%@,地理位置資訊%@",location,region,addressDic); /* // NSString *name=placemark.name;//地名 // NSString *thoroughfare=placemark.thoroughfare;//街道 // NSString *subThoroughfare=placemark.subThoroughfare; //街道相關資訊,例如門牌等 // NSString *locality=placemark.locality; // 城市 // NSString *subLocality=placemark.subLocality; // 城市相關資訊,例如標誌性建築 // NSString *administrativeArea=placemark.administrativeArea; // 州 // NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政地區資訊 // NSString *postalCode=placemark.postalCode; //郵編 // NSString *ISOcountryCode=placemark.ISOcountryCode; //國家編碼 // NSString *country=placemark.country; //國家 // NSString *inlandWater=placemark.inlandWater; //水源、湖泊 // NSString *ocean=placemark.ocean; // 海洋 // NSArray *areasOfInterest=placemark.areasOfInterest; //關聯的或利益相關的地標 */ }];}
根據經緯度反編碼取出地址(反編碼)
- (void)getAdressByLongitude:(CLLocationDegrees)longitude Latitude:(CLLocationDegrees)latitude{ //反編碼 //建立Cllocation CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]; [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { NSDictionary *dic = placemarks.firstObject.addressDictionary; NSLog(@"反編碼地理位置資訊:%@",dic); }];}
計算兩點之間的距離
- (void)distance{ //建立位置一 CLLocation *locationBeijing = [[CLLocation alloc] initWithLatitude:40 longitude:116]; CLLocation *locationDaLiang = [[CLLocation alloc] initWithLatitude:39 longitude:121]; CLLocationDistance distance = [locationBeijing distanceFromLocation:locationDaLiang]; NSLog(@"北京到大連的距離:%f",distance);}
iOS學習_地圖_定位和編碼與反編碼