IOS 地理编码以及反地理编码

//git

//  ViewController.m编码

//  地理编码以及反地理编码spa

//.net

//  Created by dc008 on 15/12/23.3d

//  Copyright © 2015 崔晓宇. All rights reserved.code

//orm


//搜索的全部结果都是在中国境内的,由于苹果在中国的地图服务商是高德地图对象


#import "ViewController.h"get

#import <CoreLocation/CoreLocation.h>it

@interface ViewController ()

{

    CLGeocoder  *_geoCoder;//建立地理编码对象

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    _geoCoder = [[CLGeocoder alloc]init];

    //地理编码...传入的信息是地名

    

    [self getCoordinateByAddress:@"独山子"];

    [self getAddressByLatitude:44.32720700 longitude:84.88226700];

    

    //反地理编码...传入的信息是坐标(经纬度)

}


#pragma mark 根据地名肯定地理坐标

- (void)getCoordinateByAddress : (NSString *)address {


    [_geoCoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        //取得地标,地标中存储了详细的地址信息。注意:一个地名能够搜索出多个地址

        CLPlacemark *placemark = [placemarks firstObject];

//        NSLog(@"%@",placemark);

        //剖析地标

        //位置

        CLLocation * location = placemark.location;

        NSLog(@"%@",location);

        //区域

        CLRegion *region = placemark.region;

//        NSLog(@"%@",region);

        //详细地址

        NSDictionary *addressDic = placemark.addressDictionary;

        NSLog(@"%@",addressDic);

        

//        NSLog(@"%@",addressDic);//详细地址

//        NSLog(@"%@",region);//区域

//        NSLog(@"%@",location);//位置

    }];

}


#pragma mark 根据坐标取得地名

- (void)getAddressByLatitude : (CLLocationDegrees)latitude longitude : (CLLocationDegrees)longitude{

    //经过经纬度建立位置信息

    CLLocation *location = [[CLLocation alloc]initWithLatitude:latitude longitude:longitude];

    [_geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        //获取地标

        CLPlacemark *placemark = [placemarks firstObject];

        NSLog(@"%@",placemark.addressDictionary[@"Name"]);

    }];


}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

相关文章
相关标签/搜索