zoukankan      html  css  js  c++  java
  • iOS 地理编码 / 反地理编码

    一、CLGeocoder 地理编码 与 反地理编码

    • 地理编码
      • 根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
      // 地理编码方法
        -(void)geocodeAddressString:(NSString*)addressStringcompletionHandler:(CLGeocodeCompletionHandler)completionHandler;
    • 反地理编码:
      • 根据给定的经纬度,获得具体的位置信息
      // 反地理编码方法
      -(void)reverseGeocodeLocation:(CLLocation*)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
      + 注意:CLGeocodeCompletionHandler - 当地理反地理编码完成时,就会`调用CLGeocodeCompletionHandler`,可以`获取到CLPlacemark对象` ```objc // 这个block传递2个参数 // error:当编码出错时(比如编码不出具体的信息)有值 // placemarks:里面装着CLPlacemark对象 typedef void(^CLGeocodeCompletionHandler) (NSArray*placemarks, NSError*error);
    • CLPlacemark(locality:城市名称 thoroughfare:街道名称 name:全称 CLLocation *location)

    二、应用场景

    • 与定位结合使用,用于确定当前用户所在的具体地址信息

    三、实例

    #import "ViewController.h"
    #import <CoreLocation/CoreLocation.h>
    
    @interface ViewController ()
    
    
    /** 地理编码 */
    @property (nonatomic, strong) CLGeocoder *geoC;
    @property (weak, nonatomic) IBOutlet UITextView *addressTV;
    
    @property (weak, nonatomic) IBOutlet UITextField *latitudeTF;
    
    @property (weak, nonatomic) IBOutlet UITextField *longitudeTF;
    @end
    
    @implementation ViewController
    
    
    #pragma mark -懒加载
    -(CLGeocoder *)geoC
    {
        if (!_geoC) {
            _geoC = [[CLGeocoder alloc] init];
        }
        return _geoC;
    }
    
    
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        //
        [self.view endEditing:YES];
    }
    
    /**
     *  地理编码(地址转经纬度)
     */
    - (IBAction)geoCoder {
        
        NSString *address = self.addressTV.text;
        
        // 容错
        if([address length] == 0)
            return;
        
        [self.geoC geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            
            // CLPlacemark : 地标
            // location : 位置对象
            // addressDictionary : 地址字典
            // name : 地址详情
            // locality : 城市
            
            if(error == nil)
            {
                CLPlacemark *pl = [placemarks firstObject];
                self.addressTV.text = pl.name;
                self.latitudeTF.text = @(pl.location.coordinate.latitude).stringValue;
                self.longitudeTF.text = @(pl.location.coordinate.longitude).stringValue;
            }else
            {
                NSLog(@"错误");
            }
        }];
    }
    - (IBAction)reverseGeoCoder {
        
        // 获取用户输入的经纬度
        double latitude = [self.latitudeTF.text doubleValue];
        double longitude = [self.longitudeTF.text doubleValue];
        
        CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
        
        // 反地理编码(经纬度---地址)
        [self.geoC reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            if(error == nil)
            {
                CLPlacemark *pl = [placemarks firstObject];
                self.addressTV.text = pl.name;
                self.latitudeTF.text = @(pl.location.coordinate.latitude).stringValue;
                self.longitudeTF.text = @(pl.location.coordinate.longitude).stringValue;
            }else
            {
                NSLog(@"错误");
            }
        }];
    }
    @end
     
  • 相关阅读:
    JAVA_WEB--jsp概述
    npr_news英语新闻听力——每日更新
    词根词缀高效背单词技巧--词霸天下完整版
    python刷LeetCode:1071. 字符串的最大公因子
    python刷LeetCode:1013. 将数组分成和相等的三个部分
    python刷LeetCode:543. 二叉树的直径
    python刷LeetCode:121. 买卖股票的最佳时机
    python刷LeetCode:38. 外观数列
    python刷LeetCode:35. 搜索插入位置
    python刷LeetCode:28. 实现 strStr()
  • 原文地址:https://www.cnblogs.com/jiqiaochun/p/8548352.html
Copyright © 2011-2022 走看看