高德地圖《解析》














#import

- (void)viewDidLoad {

[super viewDidLoad];

_tv.backgroundColor = [UIColor yellowColor];

self.geocoder = [[CLGeocoder alloc] init];

}

- (IBAction)btn1:(UIButton *)sender {

//? ? __block ViewController *weakSelf = self;

NSString *addr = self.tf1.text;

if (addr != nil && addr.length > 0) {

[self.geocoder geocodeAddressString:self.tf1.text completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {

if (placemarks.count > 0) {

CLPlacemark *place = placemarks[0];

CLLocation *location = place.location;

self.tv.text = [NSString stringWithFormat:@"%@經(jīng)度為:%g,緯度為:%g",addr,location.coordinate.longitude,location.coordinate.latitude];

//? ? ? ? ? ? CLLocationCoordinate2D coor = location.coordinate;

//? ? ? ? ? ? [self.tv setText:@"緯度:%@\n經(jīng)度:%@",coor.location,];

}else

{

[[[UIAlertView alloc] initWithTitle:@"提示框" message:@"地址無法解析" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil] show];

}

}];

}}

- (IBAction)btn2:(UIButton *)sender {

NSString *longitudeStr = self.tf2.text;

NSString *latitudeStr = self.tf3.text;

if (longitudeStr != nil && longitudeStr.length > 0 && latitudeStr != nil && latitudeStr.length > 0) {

CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitudeStr floatValue] longitude:[longitudeStr floatValue]];

[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {

if (placemarks.count > 0) {

CLPlacemark *placemark = placemarks[0];

NSArray *addrArray = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];

NSMutableString *addr = [[NSMutableString alloc] init];

for (int i = 0; i < addrArray.count; i++) {

[addr appendString:addrArray[i]];

}

self.tv.text = [NSString stringWithFormat:@"經(jīng)度:%g,緯度:%g的地址為:%@",location.coordinate.longitude,location.coordinate.latitude,addr];

}

}];

}

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容