美文网首页
iOS开发之地理编码 反地理编码

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

作者: 安静SRR | 来源:发表于2016-04-20 18:00 被阅读384次

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

//
//  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

相关文章

  • iOS开发 - 地理编码与反地理编码

    在获取用户位置时候,Core Location 会返回一对经纬度。我们人类看不出什么东东,但是存在就是合理,交给机...

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

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

  • CLGeocoder

    CLGeocoder(地理编码) 使用CLGeocoder可以完成“地理编码”和“反地理编码”地理编码:根据给定的...

  • iOS 地理编码 / 反地理编码

    一、CLGeocoder 地理编码 与 反地理编码 地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址...

  • 地理编码

    地理编码和反地理编码都使用CLGeocoder类来实现. 地理编码使用 geocodeAddressString:...

  • 地图和定位(三)

    一、地理编码和反地理编码 地理编码:把地址转为经纬度反地理编码:把经纬度转为地址 二、获取当前城市名称(定位+反地...

  • iOS - 定位、地理编码、反地理编码

    #pragma mark---*定位: 一、介绍 1、定位使用CoreLocation框架 2、功能:(1)基础定...

  • iOS 地理编码反编码

    本文目录 地理编码地名 -> 经纬度 等具体位置数据信息。根据给定的位置(通常是地名)确定地理坐标(经、纬度)。 ...

  • 基于CLGeocoder - 反地理编码

    iOS中CoreLocatio框架中的CLGeocoder 类不但为我们提供了地理编码方法,而且还提供了反地理编码...

  • 基于百度API的 - 反地理编码

    iOS中CoreLocatio框架中的CLGeocoder 类不但为我们提供了地理编码方法,而且还提供了反地理编码...

网友评论

      本文标题:iOS开发之地理编码 反地理编码

      本文链接:https://www.haomeiwen.com/subject/hhzzlttx.html