ios学习笔记之地图(中)

一 前言

本章主要介绍地理编码及反地理编码,需要用到CLGeocoder类

二 地理编码

在下图绿框内输入地址,点击地理编码,显示经度和纬度

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *addressTV;// 那个绿框textView
@property (weak, nonatomic) IBOutlet UITextField *longTF;//经度的field
@property (weak, nonatomic) IBOutlet UITextField *laTF;//纬度的field
//地理编码
@property (nonatomic, strong) CLGeocoder *geoC;
@end

@implementation ViewController

#pragma mark - 懒加载
- (CLGeocoder *)geoC{
    if (!_geoC) {
        _geoC = [[CLGeocoder alloc] init];
    }
    
    return _geoC;
}

//点击“地理编码”按钮
- (IBAction)geoCode {
    
    NSString *str = self.addressTV.text;
    
    if ([str length] == 0) {
        return ;
    }
    
    
//    编码其实是去向苹果服务器请求数据
    [self.geoC geocodeAddressString:str completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        /**
         * CLPlacemark
         *
         */
        
        if (!error) {
            NSLog(@"%@",placemarks);
//            遍历一下这个数组,因为可能有多个查询结果被返回
            [placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSLog(@"name = %@",obj.name);
                self.addressTV.text = obj.name;
//                self.laTF.text = [NSString stringWithFormat:@"%f",obj.location.coordinate.latitude];
//                self.longTF.text = [NSString stringWithFormat:@"%f",obj.location.coordinate.longitude];
                self.laTF.text = @(obj.location.coordinate.latitude).stringValue;//获obj.location.coordinate.latitude的字符串格式
                self.longTF.text = @(obj.location.coordinate.longitude).stringValue;
                
            }];
            
        }else{
            NSLog(@"error = %@",error);
        }
    }];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
三 反地理编码

在上图输入经度和纬度的地方,分别输入经度和纬度,点击反地理编码,在绿框内显示地址

//点击“反地理编码”按钮
- (IBAction)reverseGeoCode {
    
    CLLocationDegrees latitude = [self.laTF.text doubleValue];
    CLLocationDegrees longtitude = [self.longTF.text doubleValue];
    
//    TODO: 容错,防止用户输入非经纬度的内容
    
//    if (!) {
//        <#statements#>
//    }
    
    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longtitude];
    [self.geoC reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        if (!error) {
            
            NSLog(@"%@",placemarks);
            [placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                self.addressTV.text = obj.name;
            }];
            
            
        }else{
            NSLog(@"无法正常编码");
        }
        
        
    }];
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、简单说明 CLGeocoder:地理编码器,其中Geo是地理的英文单词Geography的简写。 1.使用CL...
    行走的菜谱阅读 3,736评论 0 0
  • 出自http://my.oschina.net/are1OfBlog/blog/420034 摘要 现在很多社交、...
    JJO阅读 9,627评论 4 19
  • http://www.cnblogs.com/kenshincui/p/4125570.html 摘要: 现在很多...
    大崔老师阅读 8,588评论 1 2
  • *初学地图时,觉得地图是个很高深的玩意儿,导航、定位、检索这得运用多少算法和核心动画的知识点啊,于是一直排斥 * ...
    柳骏阅读 9,327评论 11 22
  • 夜,深邃如墨, 微弱的月光撒在头顶,伴着风和云。 我想拾起一缕这般惊心动魄的皎洁, 它却从指尖里溜走。 原来美的如...
    姓名阅读 2,994评论 0 8

友情链接更多精彩内容