地理编码与反地理编码
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//地理编码
[self geocoder:nil];
//反地理编码;
}
//没联网,没办法执行
-(void)geocoder:(UIButton*)sender
{
//初始化地理编码类
CLGeocoder *geo=[[CLGeocoder alloc]init];
//开始地理编码
[geo geocodeAddressString:@"北京" completionHandler:^(NSArray *placemarks, NSError *error) {
if(placemarks.count==0||error)
NSLog(@"%@",error) ;
for (CLPlacemark *mark in placemarks) {
NSLog(@"%f ,%f ,%@, %@",mark.location.coordinate.latitude,mark.location.coordinate.longitude,mark.name, mark.description);
}
}];
NSLog(@"===");
}
//反地理编码
-(void)diGeoCoder
{
CLLocation *location=[[CLLocation alloc]initWithLatitude:38.1 longitude:116];
CLGeocoder *geo=[[CLGeocoder alloc]init];
[geo reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark *mark in placemarks) {
NSLog(@"%f ,%f ,%@, %@",mark.location.coordinate.latitude,mark.location.coordinate.longitude,mark.name, mark.description);
}
}];
}