CoreLocation.framework 要用到这个框架
import <CoreLocation/CoreLocation.h>
地图编码 是 向苹果发送一个地名 返回详细的地名
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()
/**
* 地理编码对象
*/
@property (nonatomic,strong) CLGeocoder * geocoder;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.geocoder = [[CLGeocoder alloc] init];
// 向苹果服务器发送网络请求,获取 "地名" 的详细信息
//广州图书馆
[self.geocoder geocodeAddressString:@"浸潭镇" completionHandler:^(NSArray *placemarks, NSError *error) {
//此block是在主线程调用
NSLog(@"%@",[NSThread currentThread]);
if (!error) {
// CLPlacemark 位置标记
for (CLPlacemark *placemark in placemarks) {
NSLog(@"成功 %@",[placemark class]);
NSLog(@"地理名称%@",placemark.name);
NSLog(@"街道名%@",placemark.thoroughfare);
NSLog(@"国家%@",placemark.country);
NSLog(@"城市%@",placemark.locality);
NSLog(@"区: %@",placemark.subLocality);
NSLog(@"地址%@",placemark.addressDictionary);
NSLog(@"=======\n");
}
}else{
NSLog(@"%@",error);
}
}];
}
@end
反地理编码 是 向苹果发送一个经纬度 返回详细的地名
对象懒加载
import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()
@property (nonatomic,strong) CLGeocoder * geocoder;
@end
@implementation ViewController
-(CLGeocoder *)geocoder{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//反地理编码: 把 "经纬" 转化成详细的地理信息
//广州花城广场: 经度 113.324675 纬度 23.124103
CLLocation *loc = [[CLLocation alloc] initWithLatitude:23.124103 longitude:113.324675];
[self.geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {
//此block是在主线程调用
NSLog(@"%@",[NSThread currentThread]);
if (!error) {
// CLPlacemark 位置标记
for (CLPlacemark *placemark in placemarks) {
NSLog(@"成功 %@",[placemark class]);
NSLog(@"地理名称%@",placemark.name);
NSLog(@"街道名%@",placemark.thoroughfare);
NSLog(@"国家%@",placemark.country);
NSLog(@"城市%@",placemark.locality);
NSLog(@"区: %@",placemark.subLocality);
NSLog(@"地址%@",placemark.addressDictionary);
NSLog(@"=======\n");
}
}else{
NSLog(@"%@",error);
}
}];
}
@end