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

//

//  ViewController.m

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

//

//  Created by dc008 on 15/12/23.

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

//


//搜索的所有结果都是在中国境内的,因为苹果在中国的地图服务商是高德地图


#import "ViewController.h"

#import <CoreLocation/CoreLocation.h>

@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


转载于:https://my.oschina.net/u/2499773/blog/552467

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值