#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()
/**
* 需要编码的地址容器
*/
@property (weak, nonatomic) IBOutlet UITextField *addressField;
/**
* 经度容器
*/
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
/**
* 纬度容器
*/
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
/**
* 详情容器
*/
@property (strong, nonatomic) IBOutlet UITextField *detailAddressView;
/**
* 地理编码对象
*/
@property (nonatomic, strong) CLGeocoder *geocoder;
@end
@implementation ViewController
- (IBAction)geocodeBtnClick:(id)sender {
// 0、获取地理位置
NSString *placeStr = self.addressField.text;
// 1.创建地理编码对象
// 2.利用地理编码对象编码
// 根据传入的地址获取该地址对应的经纬度信息
[self.geocoder geocodeAddressString:placeStr completionHandler:^(NSArray *placemarks, NSError *error) {
// placemarks地标数组, 地标数组中存放着地标, 每一个地标包含了该位置的经纬度以及城市/区域/国家代码/邮编等等...
// 获取数组中的第一个地标
CLPlacemark *placemark = [placemarks firstObject];
// for (CLPlacemark *placemark in placemarks) {
// NSLog(@"%@ %@ %f %f", placemark.name, placemark.addressDictionary, placemark.location.coordinate.latitude, placemark.location.coordinate.longitude);
self.latitudeLabel.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.latitude];
self.longitudeLabel.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.longitude];
NSArray *address = placemark.addressDictionary[@"FormattedAddressLines"];
NSMutableString *strM = [NSMutableString string];
for (NSString *str in address) {
[strM appendString:str];
}
self.detailAddressView.text = strM;
}];
}
- (CLGeocoder *)geocoder {
if (_geocoder == nil) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}
@end
iOS开发-地图03-地理编码
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 在我们的生活中现在很多App大多都可以获取地理位置进行相关的定位标记等,有的例如餐饮App,当我们需要订餐时,我们...
- 在获取用户位置时候,Core Location 会返回一对经纬度。我们人类看不出什么东东,但是存在就是合理,交给机...
- 什么是地理编码和反地理编码? 地理编码 地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)。...
- 一、下载最新百度地图sdk,导入工程中 二、根据官方文档初始化地图,在main.xml中添加对应布局 //获取地图...