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

相關文章
相關標籤/搜索