zoukankan      html  css  js  c++  java
  • GeocodeLocation

    #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
    

      

  • 相关阅读:
    实现h5中radio单击取消与选中
    小程序中的组件化理解
    阿里字体css代码引入方法
    前端布局心得小结
    Python学习资源汇总,转载自他人
    史上最全 原生javascript的知识总结,适合新手及查资料用!
    windows Python 3.4.3 安装图文
    PyInstaller编译python3时使用的详细参数介绍
    PyInstaller 安装方法 及简单的编译exe (python3)
    Windows 安装 GTK+ 图文说明
  • 原文地址:https://www.cnblogs.com/yedayi/p/5137683.html
Copyright © 2011-2022 走看看