地理編碼 與 反地理編碼:git
#import "ViewController.h" //先引入庫 #import <CoreLocation/CoreLocation.h> @interface ViewController () //建立一個CLGeocoder @property (nonatomic, strong) CLGeocoder *geocoder; #pragma mark - 地理編碼 //在界面上建立控件 - (IBAction)geocode; @property (weak, nonatomic) IBOutlet UITextField *addressField; @property (weak, nonatomic) IBOutlet UILabel *longitudeLabel; @property (weak, nonatomic) IBOutlet UILabel *latitudeLabel; @property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel; #pragma mark - 反地理編碼 //在界面上建立控件 - (IBAction)reverseGeocode; @property (weak, nonatomic) IBOutlet UITextField *longtitudeField; @property (weak, nonatomic) IBOutlet UITextField *latitudeField; @property (weak, nonatomic) IBOutlet UILabel *reverseDetailAddressLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (CLGeocoder *)geocoder { if (_geocoder == nil) { _geocoder = [[CLGeocoder alloc] init]; } return _geocoder; } #pragma mark -地理編碼 //地址 ---> 經緯度 - (void)geocode { NSString *address = self.addressField.text; if (self.addressField.text.length == 0) { return; } [self.geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { if (error || placemarks.count == 0) { self.addressField.text = @"您輸入的位置存在錯誤"; } else { for (CLPlacemark *placemark in placemarks) { NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@", placemark.name, placemark.locality, placemark.country, placemark.postalCode); } /* thoroughfare 街道 locality 城市 subLocality 街道 postalCode 郵編 ISOcountryCode 國家編碼 country 國家 */ //顯示最前面的地標 CLPlacemark *placemark = [placemarks firstObject]; self.longitudeLabel.text = [NSString stringWithFormat:@"%.2f",placemark.location.coordinate.longitude]; self.latitudeLabel.text = [NSString stringWithFormat:@"%.2f",placemark.location.coordinate.latitude]; self.detailAddressLabel.text = placemark.name; } }]; } #pragma mark -反地理編碼 //經緯度 --> 地名 - (void)reverseGeocode { CLLocation *location = [[CLLocation alloc] initWithLatitude:[self.latitudeLabel.text doubleValue] longitude:[self.longitudeLabel.text doubleValue]]; [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { if (error || placemarks.count == 0) { self.reverseDetailAddressLabel.text = @"您輸入的經緯度存在錯誤"; } else { for (CLPlacemark *placemark in placemarks) { NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@", placemark.name, placemark.locality, placemark.country, placemark.postalCode); } CLPlacemark *placemark = [placemarks firstObject]; self.reverseDetailAddressLabel.text = placemark.name; self.longtitudeField.text = [NSString stringWithFormat:@"%.2f",placemark.location.coordinate.longitude]; self.latitudeField.text = [NSString stringWithFormat:@"%.2f",placemark.location.coordinate.latitude]; } }]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; }
OC庫定位:網絡
#import "ViewController.h" //引入庫 #import "CoreLocation/CoreLocation.h" @interface ViewController ()<CLLocationManagerDelegate>{ CLLocationManager *manager; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; if ([CLLocationManager locationServicesEnabled]) { //能夠定位並開始定位 [self.manager startUpdatingLocation]; //傳入一個區域 //[self.manager startMonitoringForRegion:<#(nonnull CLRegion *)#>]; }else{ //提醒用戶打開定位開關 //檢查網絡 } //計算距離 [self countDistance]; } //定位的方法 -(CLLocationManager *)manager{ if (manager == nil) { //建立用戶管理器,定位用戶的位置 manager = [[CLLocationManager alloc]init]; //判斷當前的版本 if ([[[UIDevice currentDevice] systemVersion]floatValue] >= 8.0) { [manager requestAlwaysAuthorization]; [manager requestWhenInUseAuthorization]; } manager.delegate = self; //每隔多少米定位一次 manager.distanceFilter = kCLDistanceFilterNone; //精確度 manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; } return manager; } //計算距離的方法 -(void)countDistance{ CLLocation *lo1 = [[CLLocation alloc]initWithLatitude:41 longitude:115]; CLLocation *lo2 = [[CLLocation alloc]initWithLatitude:41 longitude:116]; CLLocationDistance distance = [lo1 distanceFromLocation:lo2]; NSLog(@"%f", distance); } //定位本身的位置 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{ CLLocation *location = [locations lastObject]; //緯度 location.coordinate.latitude //經度 location.coordinate.longitude NSLog(@"緯度%f------經度%f",location.coordinate.latitude,location.coordinate.longitude); //中止更新位置(很是耗電) // [manager stopUpdatingLocation]; } //進入某個區域調用 - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{ } //離開某個區域調用 - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{ }
iOS地圖(MapKit):post
#import "ViewController.h" //先引入庫 #import <MapKit/MapKit.h> //注意引入代理 @interface ViewController ()<MKMapViewDelegate> //新建一個CLLocationManager @property (nonatomic,strong) CLLocationManager *manager; @end @implementation ViewController /** * !!!!!!!!!注意:要在 Info.plist 中添加 NSLocationWhenInUseUsageDescription ,並把 Boolean 改爲 YES */ - (void)viewDidLoad { [super viewDidLoad]; //判斷當前版本(8.0之後使用地圖須要先定位) if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 8.0) { self.manager = [[CLLocationManager alloc]init]; [self.manager requestWhenInUseAuthorization]; [self.manager requestAlwaysAuthorization]; } //初始化地圖 MKMapView *mapView = [[MKMapView alloc]initWithFrame:self.view.bounds]; //設置地圖的類型 mapView.mapType = MKMapTypeStandard; //設置跟蹤模式 mapView.userTrackingMode = MKUserTrackingModeFollow; //設置代理(監控地圖的相關行爲:好比顯示的區域發生了改變) mapView.delegate = self; [self.view addSubview:mapView]; } /** * 更新到用戶的位置時就會調用(顯示的位置、顯示範圍改變) * userLocation : 大頭針模型數據, 對大頭針位置的一個封裝(這裏的userLocation描述的是用來顯示用戶位置的藍色大頭針) */ -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{ //地圖上的大頭針上的簡介 userLocation.title = @"哈哈哈哈哈哈"; userLocation.subtitle = @"我是介紹"; } /** * 地圖顯示的區域即將改變了就會調用 */ -(void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{ NSLog(@"regionWillChangeAnimated"); } /** * 地圖顯示的區域改變了就會調用(顯示的位置、顯示範圍改變) */ -(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{ //中心點 CLLocationCoordinate2D center = mapView.region.center; //跨度 MKCoordinateSpan span = mapView.region.span; NSLog(@"中心點=(%f,%f), 區域跨度=(%f,%f)", center.longitude,center.latitude,span.longitudeDelta,span.latitudeDelta); }