iOS -CoreLocation框架(反)地理編碼

#import "ViewController.h"#import@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextView *addressTV;

@property (weak, nonatomic) IBOutlet UITextField *laTF;

@property (weak, nonatomic) IBOutlet UITextField *longTF;

/** 地理編碼 */

@property (nonatomic, strong) CLGeocoder *geoC;

@end

@implementation ViewController


- (CLGeocoder *)geoC{??

? if (!_geoC) {? ? ? ?

?_geoC = [[CLGeocoder alloc] init];? ??

}? ?

?return _geoC;

}

#pragma mark -地理編碼

- (IBAction)geoCoder {? ? ? ? ?

?NSString *addr? = self.addressTV.text;? ? ??

? if ([addr length] == 0) {? ? ? ?

?return;?

?? }? ? ? ? [self.geoC geocodeAddressString:addr completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {

/**

*? CLPlacemark

location : 位置對象

addressDictionary : 地址字典

name : 地址全稱

*/

if(error == nil)

{

NSLog(@"%@", placemarks);

[placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

NSLog(@"%@", obj.name);

self.addressTV.text = obj.name;

self.laTF.text = @(obj.location.coordinate.latitude).stringValue;

self.longTF.text = @(obj.location.coordinate.longitude).stringValue;

}];

}else

{

NSLog(@"cuowu--%@", error.localizedDescription);

}

}];

}

#pragma mark- 反地理編碼

- (IBAction)reverseGeoCoder { ? ? ??? ??

?double lati = [self.laTF.text doubleValue];? ??

double longi = [self.longTF.text doubleValue];? ? ? ?

?// TODO: 容錯? ? ? ?

?CLLocation *loc = [[CLLocation alloc] initWithLatitude:lati longitude:longi];? ??

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

if(error == nil)

{

NSLog(@"%@", placemarks);

[placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

NSLog(@"%@", obj.name);

self.addressTV.text = obj.name;

self.laTF.text = @(obj.location.coordinate.latitude).stringValue;

self.longTF.text = @(obj.location.coordinate.longitude).stringValue;

}];

}else

{

NSLog(@"cuowu");

}

}];

}

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

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

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