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
    

      

  • 相关阅读:
    JXOI2018简要题解
    TJOI2018简要题解
    BJOI2018简要题解
    【题解】Luogu P4091 [HEOI2016/TJOI2016]求和
    【题解】Luogu P5301 [GXOI/GZOI2019]宝牌一大堆
    【题解】Luogu P5291 [十二省联考2019]希望
    【题解】Luogu P3349 [ZJOI2016]小星星
    【题解】Luogu P5327 [ZJOI2019]语言
    【题解】Luogu P5319 [BJOI2019]奥术神杖
    【题解】Luogu P5471 [NOI2019]弹跳
  • 原文地址:https://www.cnblogs.com/yedayi/p/5137683.html
Copyright © 2011-2022 走看看