CoreLocation.framework 要用到這個(gè)框架
import <CoreLocation/CoreLocation.h>
地圖編碼 是 向蘋果發(fā)送一個(gè)地名 返回詳細(xì)的地名
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()
/**
* 地理編碼對(duì)象
*/
@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];
// 向蘋果服務(wù)器發(fā)送網(wǎng)絡(luò)請(qǐng)求虫腋,獲取 "地名" 的詳細(xì)信息
//廣州圖書館
[self.geocoder geocodeAddressString:@"浸潭鎮(zhèn)" completionHandler:^(NSArray *placemarks, NSError *error) {
//此block是在主線程調(diào)用
NSLog(@"%@",[NSThread currentThread]);
if (!error) {
// CLPlacemark 位置標(biāo)記
for (CLPlacemark *placemark in placemarks) {
NSLog(@"成功 %@",[placemark class]);
NSLog(@"地理名稱%@",placemark.name);
NSLog(@"街道名%@",placemark.thoroughfare);
NSLog(@"國(guó)家%@",placemark.country);
NSLog(@"城市%@",placemark.locality);
NSLog(@"區(qū): %@",placemark.subLocality);
NSLog(@"地址%@",placemark.addressDictionary);
NSLog(@"=======\n");
}
}else{
NSLog(@"%@",error);
}
}];
}
@end
反地理編碼 是 向蘋果發(fā)送一個(gè)經(jīng)緯度 返回詳細(xì)的地名
對(duì)象懶加載
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.
//反地理編碼: 把 "經(jīng)緯" 轉(zhuǎn)化成詳細(xì)的地理信息
//廣州花城廣場(chǎng): 經(jīng)度 113.324675 緯度 23.124103
CLLocation *loc = [[CLLocation alloc] initWithLatitude:23.124103 longitude:113.324675];
[self.geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {
//此block是在主線程調(diào)用
NSLog(@"%@",[NSThread currentThread]);
if (!error) {
// CLPlacemark 位置標(biāo)記
for (CLPlacemark *placemark in placemarks) {
NSLog(@"成功 %@",[placemark class]);
NSLog(@"地理名稱%@",placemark.name);
NSLog(@"街道名%@",placemark.thoroughfare);
NSLog(@"國(guó)家%@",placemark.country);
NSLog(@"城市%@",placemark.locality);
NSLog(@"區(qū): %@",placemark.subLocality);
NSLog(@"地址%@",placemark.addressDictionary);
NSLog(@"=======\n");
}
}else{
NSLog(@"%@",error);
}
}];
}
@end