// // ViewController.m // location_Core Location // // Created by bin chen on 16/1/26. // Copyright © 2016年 bin chen. All rights reserved. // #import "ViewController.h" //導入接口文件 #import <CoreLocation/CoreLocation.h> @interface ViewController ()<CLLocationManagerDelegate> @property (nonatomic,strong) CLLocationManager * locationManager; @property (nonatomic,assign) float latitude; @property (nonatomic,assign) float longitude; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //位置管理器實例 self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; //使用前判斷版本號 if ([[[UIDevice currentDevice] systemVersion]floatValue] >= 8.0) { //調用受權方法 [self.locationManager requestWhenInUseAuthorization]; } self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; //指定位置精度 self.locationManager.distanceFilter = 10.0f; //每移動200米發送一次更新給委託,即距離過濾器 [self.locationManager startUpdatingLocation]; //啓動位置更新,除非設置中止,不然一旦啓動將會一直髮送更新,即實時更新 // [self.locationManager stopUpdatingLocation]; //定位成功後中止定位 } #pragma mark - 與位置相關的 -CLLocationManagerDelegate 方法 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations{ //locations 位置變化的集合, 其中每一個CLLocation對象都包含:coordinate(座標)、altitude(海拔高度)、horizontalAccuracy(位置的精度-半徑)、verticalAccuracy(海拔高度的精度)、speed(速度) CLLocation *curLocation = [locations lastObject]; //最新位置信息 if (curLocation.horizontalAccuracy > 0) { NSLog(@"當前位置:%f,%f +/- %f meters", curLocation.coordinate.longitude, curLocation.coordinate.latitude, curLocation.horizontalAccuracy); //設一個目的地北京,計算模擬器中定位的香港到北京的距離 CLLocation *destLocation = [[CLLocation alloc] initWithLatitude:39.904989 longitude:116.405285]; CLLocationDistance distance = [destLocation distanceFromLocation:curLocation]; NSLog(@"兩地之間的距離: %f", distance); if (distance < 500) { [self.locationManager stopUpdatingLocation]; //中止定位 NSLog(@"您已經到達目的地"); } self.latitude = curLocation.coordinate.latitude; self.longitude = curLocation.coordinate.longitude; //反編碼 [self createCity]; } if (curLocation.verticalAccuracy > 0) { NSLog(@"當前海拔高度%f +/- %f meters", curLocation.altitude, curLocation.verticalAccuracy); } } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(nonnull NSError *)error{ if (error.code == kCLErrorLocationUnknown) { //沒法確認位置 NSLog(@"Currently unable to retrieve location"); }else if (error.code == kCLErrorNetwork){ //沒法定位,請檢查網絡是否鏈接 NSLog(@"Network used to retrieve location is unavailable."); }else if (error.code == kCLErrorDenied){ //用戶禁止應用程序定位 NSLog(@"Permission to retrieve location is denied."); [manager stopUpdatingLocation]; //中止定位 } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - 反編碼 - (void)createCity{ //地理編碼 ,反編碼 //經緯度--->位置信息 CLGeocoder *gecoder = [[CLGeocoder alloc] init]; CLLocation *location = [[CLLocation alloc]initWithLatitude: self.latitude longitude:self.longitude]; // CLLocation *location = [[CLLocation alloc]initWithLatitude: 39.904989 longitude:116.405285]; [gecoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { if (error) { NSLog(@"反編碼失敗:%@",error); }else{ NSLog(@"反編碼成功"); //NSLog(@"%@",placemarks); CLPlacemark *placemark = [placemarks lastObject]; NSLog(@"%@ %@",placemark.name,placemark.thoroughfare); NSLog(@"%@",placemark.name); NSLog(@"%@",placemark.locality); } }]; } #pragma mark - 編碼 - (void)ReginWithCity:(NSString *)cityName{ CLGeocoder *gecoder = [[CLGeocoder alloc] init]; [gecoder geocodeAddressString:cityName completionHandler:^(NSArray *placemarks, NSError *error) { if (error) { NSLog(@"編碼失敗:%@",error); }else{ NSLog(@"編碼成功"); CLPlacemark *place = [placemarks lastObject]; CLLocationCoordinate2D coordinate = place.location.coordinate; NSLog(@"%f,%f",coordinate.latitude,coordinate.longitude); } }]; } @end