当前位置: 首页 > news >正文

LocationCoder 地图经纬度解析

LocationCoder 地图经纬度解析

 

其实,在地图里面将地图解析成有意义的地址,或者把地址转换成有意义的经纬度都是很容易的事情,只是我将其封装了支持KVO,通知中心,block取结果,代理取结果而已.

能通过组合的方式来封装对象扩展功能就绝对不会用继承的方式来扩展功能,只有当组合解决不了问题时才会使用继承:).

源码:

LocationCoder.h + LocationCoder.m

//
//  LocationCoder.h
//
//  http://home.cnblogs.com/u/YouXianMing/
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@class LocationCoder;

#pragma mark - block
typedef void(^resultBlock_t)(NSArray *placemarks, NSError *error, LocationCoder *locationCoder);

#pragma mark - 代理
@protocol LocationCoderDelegate <NSObject>
@optional
- (void)resultForPlacemarks:(NSArray *)placemarks
                      error:(NSError *)error
              locationCoder:(LocationCoder *)locationCoder;
@end


@interface LocationCoder : NSObject

// 初始化时设定的值
@property (nonatomic, strong, readwrite) CLLocation    *location;            // 经纬度地址
@property (nonatomic, strong, readwrite) NSString      *addressString;       // 文字描述地址

// block方式取结果
@property (nonatomic, copy)              resultBlock_t  resultBlock;         // 结果的block

// 代理方式取结果
@property (nonatomic, assign)            id<LocationCoderDelegate> delegate; // 结果代理

// KVO方式取结果
@property (nonatomic, strong, readonly)  NSString      *changeFlag;          // 用于KVO
@property (nonatomic, strong, readonly)  NSArray       *placemarks;          // 结果

// 单个的结果
@property (nonatomic, assign, readonly)  CLLocationCoordinate2D  coordinate2D;      // 经纬度
@property (nonatomic, strong, readonly)  NSString               *addressLines;      // 完整的地址
@property (nonatomic, strong, readonly)  NSDictionary           *addressDictionary; // 地址字典

// 初始化
- (instancetype)initWithLocation:(CLLocation *)location;
- (instancetype)initWithAddressString:(NSString *)string;

// 开始分析
- (void)startAnalyseLocation;       // 分析经纬度地址
- (void)startAnalyseAddressString;  // 分析文字描述地址

@end
//
//  LocationCoder.m
//
//  http://home.cnblogs.com/u/YouXianMing/
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "LocationCoder.h"

@interface LocationCoder ()

@property (nonatomic, strong) CLGeocoder *geocoder;     // 解析器
@property (nonatomic, strong) NSArray    *placemarks;
@property (nonatomic, strong) NSString   *changeFlag;   // 用于KVO

@property (nonatomic, strong) NSString                *addressLines;      // 完整的地址
@property (nonatomic, assign) CLLocationCoordinate2D   coordinate2D;      // 经纬度
@property (nonatomic, strong) NSDictionary            *addressDictionary; // 地址字典

@end

@implementation LocationCoder

- (instancetype)init
{
    return [self initWithLocation:nil];
}

- (instancetype)initWithLocation:(CLLocation *)location
{
    self = [super init];
    if (self)
    {
        _location   = location;
        _geocoder   = [[CLGeocoder alloc] init];
        _changeFlag = @"YES";
    }
    
    return self;
}

- (instancetype)initWithAddressString:(NSString *)string
{
    self = [super init];
    if (self)
    {
        _addressString = string;
        _geocoder      = [[CLGeocoder alloc] init];
        _changeFlag    = @"YES";
    }
    
    return self;
}

- (void)startAnalyseLocation
{
    if (_location)
    {
        [_geocoder reverseGeocodeLocation:_location
                        completionHandler:^(NSArray *placemarks, NSError *error)
         {
             // KVO(只有使用了setter方法才能够通知KVO)
             if (error == nil)
             {
                 self.placemarks = placemarks;
                 
                 CLPlacemark *placemark = [placemarks objectAtIndex:0];
                 
                 // 获取地址字典
                 self.addressDictionary = placemark.addressDictionary;
                 
                 // 获取详细地址
                 self.addressLines = \
                 [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                 
                 // 获取2D坐标信息
                 self.coordinate2D = placemark.location.coordinate;
                 
                 // 通知KVO
                 self.changeFlag = @"RIGHT DATA";
             }
             else
             {
                 self.placemarks = nil;
                 self.changeFlag = @"ERROR DATA";
             }
             
             // block
             if (_resultBlock)
             {
                 _resultBlock(placemarks, error, self);
             }
             
             // 代理
             if (_delegate)
             {
                 [_delegate resultForPlacemarks:placemarks
                                          error:error
                                  locationCoder:self];
             }
         }];
    }
}

- (void)startAnalyseAddressString
{
    if (_addressString)
    {
        [_geocoder geocodeAddressString:_addressString
                      completionHandler:^(NSArray *placemarks, NSError *error) {
                          // KVO(只有使用了setter方法才能够通知KVO)
                          if (error == nil)
                          {
                              self.placemarks = placemarks;
                              
                              CLPlacemark *placemark = [placemarks objectAtIndex:0];
                              
                              // 获取地址字典
                              self.addressDictionary = placemark.addressDictionary;
                              
                              // 获取详细地址
                              self.addressLines = \
                              [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                              
                              // 获取2D坐标信息
                              self.coordinate2D = placemark.location.coordinate;
                              
                              // 通知KVO
                              self.changeFlag = @"RIGHT DATA";
                          }
                          else
                          {
                              self.placemarks = nil;
                              self.changeFlag = @"ERROR DATA";
                          }
                          
                          // block
                          if (_resultBlock)
                          {
                              _resultBlock(placemarks, error, self);
                          }
                          
                          // 代理
                          if (_delegate)
                          {
                              [_delegate resultForPlacemarks:placemarks
                                                       error:error
                                               locationCoder:self];
                          }
                      }];
    }
}

@end

 

block方式解析地址

代理方式解析地址

KVO方式解析

2014-05-26 08:57:29.808 MoreMapInfo[5911:60b] latitude = 39.906031

KVO都能写,通知中心就不用说了:).

 

试试将经纬度解析成地址信息-

看看是什么地方....

其实,本人写的经纬度地址是"中国国家博物馆",囧......

 

 

 

转载于:https://www.cnblogs.com/YouXianMing/p/3752372.html

相关文章:

  • 数据库隔离级别
  • 1.3创建项目「深入浅出ASP.NET Core系列」
  • 使用API自动生成工具优化前端工作流
  • dos基本命令
  • vue双向绑定原理及实现
  • Ubuntu
  • 阿里五年Java程序员的总结,献给还在迷茫中的你!
  • log4net配置
  • vue-cli在webpack的配置文件探究
  • oracle重命名数据库
  • C语言变长数组之剖析
  • pt-tools系列:pt-online-schema-change 最佳实践
  • 项目--HTML Canvas 和 jQuery遍历
  • 美团即时物流的分布式系统架构设计
  • java虚拟机运行机制
  • ComponentOne 2017 V2版本正式发布
  • github从入门到放弃(1)
  • hadoop集群管理系统搭建规划说明
  • Javascript Math对象和Date对象常用方法详解
  • javascript 哈希表
  • js 实现textarea输入字数提示
  • laravel 用artisan创建自己的模板
  • Python - 闭包Closure
  • Synchronized 关键字使用、底层原理、JDK1.6 之后的底层优化以及 和ReenTrantLock 的对比...
  • Vue全家桶实现一个Web App
  • 给新手的新浪微博 SDK 集成教程【一】
  • 力扣(LeetCode)21
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 世界编程语言排行榜2008年06月(ActionScript 挺进20强)
  • 深度学习之轻量级神经网络在TWS蓝牙音频处理器上的部署
  • 【云吞铺子】性能抖动剖析(二)
  • # Swust 12th acm 邀请赛# [ A ] A+B problem [题解]
  • #git 撤消对文件的更改
  • #HarmonyOS:基础语法
  • #NOIP 2014#Day.2 T3 解方程
  • #pragma 指令
  • #传输# #传输数据判断#
  • (01)ORB-SLAM2源码无死角解析-(66) BA优化(g2o)→闭环线程:Optimizer::GlobalBundleAdjustemnt→全局优化
  • (4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)...
  • (delphi11最新学习资料) Object Pascal 学习笔记---第2章第五节(日期和时间)
  • (附源码)计算机毕业设计SSM教师教学质量评价系统
  • (六)vue-router+UI组件库
  • (小白学Java)Java简介和基本配置
  • (转载)CentOS查看系统信息|CentOS查看命令
  • *p++,*(p++),*++p,(*p)++区别?
  • . Flume面试题
  • ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object fil
  • .[hudsonL@cock.li].mkp勒索加密数据库完美恢复---惜分飞
  • .form文件_SSM框架文件上传篇
  • .net 4.0发布后不能正常显示图片问题
  • .NET 5.0正式发布,有什么功能特性(翻译)
  • .NET BackgroundWorker
  • .Net接口调试与案例
  • @DependsOn:解析 Spring 中的依赖关系之艺术
  • [ vulhub漏洞复现篇 ] JBOSS AS 4.x以下反序列化远程代码执行漏洞CVE-2017-7504