//// LJReveceGeocoderViewController.m// 03-正向地理編碼與反向編碼//// Created by 魯軍 on 2021/3/7.//#import "LJReveceGeocoderViewController.h"#import <CoreLocation/CoreLocation.h>@interface LJReveceGeocoderViewController ()@property(weak, nonatomic) IBOutlet UITextField *latitudeTF;@property(weak, nonatomic) IBOutlet UITextField *longitideTF;@property(weak, nonatomic) IBOutlet UILabel *cityLabel;@end@implementation LJReveceGeocoderViewController-(void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.[self.longitideTF setText:@"116"];[self.latitudeTF setText:@"40"];}-(IBAction)reveceGeocoderClick:(id)sender {NSLog(@"12312");CLGeocoder *geocoder =[CLGeocoder new];CLLocation *location1 =[[CLLocation alloc] initWithLatitude:[self.latitudeTF.text doubleValue] longitude:[self.longitideTF.text doubleValue]];[geocoder reverseGeocodeLocation:location1 completionHandler:^(NSArray<CLPlacemark *>* _Nullable placemarks, NSError * _Nullable error){if(placemarks.count ==0|| error){NSLog(@"解析error");}for(CLPlacemark *placemark in placemarks){self.cityLabel.text = placemark.locality;}}];}/*
#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*/@end//// LJGeocoderViewController.m// 03-正向地理編碼與反向編碼//// Created by 魯軍 on 2021/3/7.//#import "LJGeocoderViewController.h"#import <CoreLocation/CoreLocation.h>@interface LJGeocoderViewController ()@property(weak, nonatomic) IBOutlet UITextField *addressTF;-(IBAction)geocoderClick:(id)sender;//緯度@property(weak, nonatomic) IBOutlet UILabel *latitudeLabel;//經度@property(weak, nonatomic) IBOutlet UILabel *longitudeLabel;@property(weak, nonatomic) IBOutlet UILabel *detailAddressLabel;@property(nonatomic,strong)CLLocationManager *mgr;@end@implementation LJGeocoderViewController-(void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.self.addressTF.text =@"西三旗";}-(IBAction)geocoderClick:(id)sender {CLGeocoder *geocoder =[CLGeocoder new];[geocoder geocodeAddressString:self.addressTF.text completionHandler:^(NSArray<CLPlacemark *>* _Nullable placemarks, NSError * _Nullable error){//placemarks 地標數組if(placemarks.count==0|| error){NSLog(@"解析有錯誤");return;}for(CLPlacemark *placemark in placemarks){NSLog(@"%lf",placemark.location.coordinate.latitude);NSLog(@"%lf",placemark.location.coordinate.longitude);NSLog(@"name = %@",placemark.name);NSLog(@"locality = %@",placemark.locality);self.latitudeLabel.text =[NSString stringWithFormat:@"%f",placemark.location.coordinate.latitude];self.longitudeLabel.text=[NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude];self.detailAddressLabel.text = placemark.name;}}];}@end