iOS开发之地理编码 反地理编码

2016-04-20  本文已影响384人  安静SRR

上一章有一些地理编码 反地理编码的理论知识,有兴趣的话可以去了解一下
下面是代码示例:

//
//  ViewController.m
//  地理编码 反地理编码
//
//  Created by scsys on 16/3/8.
//  Copyright © 2016年 安静SRR. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1、地理编码:把地名转换成位置信息  用处:把文字描述的位置转换成地图上的经纬度
    //2、反地理编码:把位置信息转换成文字 用处:可以通过点击选择地图上的某一位置来获得这一位置文字的描述
    //地理编解码  在编解码的时候是一个耗时的操作  可以使用异步操作
   CLGeocoder *geocode = [[CLGeocoder alloc]init];
    
    [geocode geocodeAddressString:@"童话王国" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLPlacemark *placemark = placemarks.firstObject;
        CLLocation *loc = placemark.location;
        
        NSLog(@"经度%f纬度%f",loc.coordinate.latitude,loc.coordinate.longitude);
        //CLRegion:方圆 范围
       // CLCircularRegion:是CLRegion的子类表示圆形一个范围
        CLLocationCoordinate2D coordinate ;
        coordinate.latitude = 34;
        coordinate.longitude = 113;
        CLCircularRegion *region = [[CLCircularRegion alloc]initWithCenter:coordinate radius:kCLLocationAccuracyBest identifier:@"ll"];
        [geocode geocodeAddressString:@"童话王国" inRegion:region completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            
            CLPlacemark *placemark = placemarks.firstObject;
            CLLocation *loc = placemark.location;
            
            NSLog(@"经度%f纬度%f",loc.coordinate.latitude,loc.coordinate.longitude);

        }];
        
        
        NSLog(@"%@",[placemark.addressDictionary[@"FormattedAddressLines"] firstObject]);
    }];
/*
   使用GCD的异步操作来处理这个耗时的操作,防止阻塞主线程
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(queue, ^{
        [geocode geocodeAddressString:@"童话王国" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            CLLocation *loc = placemarks.firstObject.location;
            
                    NSLog(@"经度%f纬度%f",loc.coordinate.latitude,loc.coordinate.longitude);
            
          dispatch_queue_t main =  dispatch_get_main_queue();
            dispatch_sync(main, ^{
             //更新UI 要在主线程更新UI
            });
            
        }];
        
    });*/
    
    
    //反地理编码:把经纬度转换成地名
    CLLocation *loc = [[CLLocation alloc]initWithLatitude:34.707 longitude:113.509];
    [geocode reverseGeocodeLocation:loc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        /*
         placemarks这个数组里面只有一个元素,可以通过这个元素获得以下信息:
         <1>location:位置信息
         <2>region:范围
         <3>addressDictionary:地址信息字典
         1、name:地名
         2、thoroughfare:街道
         3、subThoroughfare:街道的副标题
         4、locality:城市
         5、subLocality:城市的相关信息
         6、administrativeArea:州 省
         7、subAdministrativeArea:州省相关信息
         8、postalCode:邮政编码
         9、ISOcountryCode:国家编码
         10、country:国家
         11、inlandWater:水源 湖泊
         12、ocean:海洋
         13、areasOfInterest:相关的地标
         */
        
        NSLog(@"%@",placemarks.firstObject.name);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

上一篇 下一篇

猜你喜欢

热点阅读