iOS MapKit导航及地理转码辅助类
头文件:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h> @interface DirectionRouteUtils : NSObject
{
MKDirections *mDirections;
CLGeocoder *mGeocoder;
} + (instancetype)sharedInstance; // 获取导航路线
- (void)findDirectionsFrom:(MKMapItem *)source
to:(MKMapItem *)destination
handler:(MKDirectionsHandler)completionHandler; - (void)findDirectionsFrom:(MKMapItem *)source
to:(MKMapItem *)destination
transportType:(MKDirectionsTransportType)transportType
handler:(MKDirectionsHandler)completionHandler; - (void)cancelCalculateDirections; // 地理转码 - (void)cancelGeocode;
- (void)geocodeAddressString:(NSString *)addressString
completionHandler:(CLGeocodeCompletionHandler)completionHandler; - (void)reverseGeocodeLocation:(CLLocation *)location
completionHandler:(CLGeocodeCompletionHandler)completionHandler; @end
实现文件:
#import "DirectionRouteUtils.h" @implementation DirectionRouteUtils + (instancetype)sharedInstance
{
static DirectionRouteUtils *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[[self class] alloc] init];
}); return sharedInstance;
} - (id)init
{
if (self = [super init]) {
mGeocoder = [[CLGeocoder alloc] init];
}
return self;
} - (void)findDirectionsFrom:(MKMapItem *)source
to:(MKMapItem *)destination
transportType:(MKDirectionsTransportType)transportType
handler:(MKDirectionsHandler)completionHandler
{
NSAssert(completionHandler != nil, @"Calculating directions handler shouldn't be nil!"); [self cancelCalculateDirections]; MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = source;
request.destination = destination;
request.requestsAlternateRoutes = YES;
request.transportType = transportType; //MKDirectionsTransportTypeAutomobile;//MKDirectionsTransportTypeWalking; mDirections = [[MKDirections alloc] initWithRequest:request];
[request release]; /*
[directions calculateDirectionsWithCompletionHandler:
^(MKDirectionsResponse *response, NSError *error) { if (error) { NSLog(@"error:%@", error);
}
else {
NSLog(@"%@", response.routes);
MKRoute *route = response.routes[0]; for(MKRoute *step in route.steps)
{
NSLog(@"Step: %@", ((MKRouteStep *)step).instructions);
} [self.mapView addOverlay:route.polyline];
}
}];
*/ if(completionHandler){
[mDirections calculateDirectionsWithCompletionHandler:completionHandler];
} } - (void)findDirectionsFrom:(MKMapItem *)source
to:(MKMapItem *)destination
handler:(MKDirectionsHandler)completionHandler
{
[self findDirectionsFrom:source
to:destination
transportType:MKDirectionsTransportTypeAutomobile
handler:completionHandler];
} - (void)cancelCalculateDirections
{
if(mDirections){
[mDirections cancel];
[mDirections release];
mDirections = nil;
}
} - (void)cancelGeocode
{
[mGeocoder cancelGeocode];
} - (void)geocodeAddressString:(NSString *)addressString
completionHandler:(CLGeocodeCompletionHandler)completionHandler
{
NSAssert(completionHandler != nil, @"Geocoding handler shouldn't be nil!"); [self cancelGeocode];
[mGeocoder geocodeAddressString:addressString
completionHandler:completionHandler];
} - (void)reverseGeocodeLocation:(CLLocation *)location
completionHandler:(CLGeocodeCompletionHandler)completionHandler
{
NSAssert(completionHandler != nil, @"Reversegeocoding handler shouldn't be nil!"); [self cancelGeocode];
[mGeocoder reverseGeocodeLocation:location
completionHandler:completionHandler]; } @end
測试用例:
- (void)testGeocoding
{
DirectionRouteUtils *utils = [DirectionRouteUtils sharedInstance];
[utils geocodeAddressString:@"你要測试的地址" completionHandler:^(NSArray *placemarks, NSError *error) {
for(CLPlacemark *mark in placemarks){
NSLog(@"%@", mark.addressDictionary);
}
}]; } - (void)testReverseGeocoding
{
DirectionRouteUtils *utils = [DirectionRouteUtils sharedInstance];
CLLocation *location = [[CLLocation alloc] initWithLatitude:24.6182746 longitude:118.131588]; [utils reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { for(CLPlacemark *mark in placemarks){
NSLog(@"%@", mark.addressDictionary);
NSLog(@"%@", mark);
} }];
[location release];
} - (void)testDirections
{ CLLocationCoordinate2D fromCoordinate = CLLocationCoordinate2DMake(24.6382086,
118.131588);
CLLocationCoordinate2D toCoordinate = CLLocationCoordinate2DMake(24.6182746,
118.131588); MKPlacemark *fromPlacemark = [[MKPlacemark alloc] initWithCoordinate:fromCoordinate
addressDictionary:nil]; MKPlacemark *toPlacemark = [[MKPlacemark alloc] initWithCoordinate:toCoordinate
addressDictionary:nil]; MKMapItem *fromItem = [[MKMapItem alloc] initWithPlacemark:fromPlacemark];
MKMapItem *toItem = [[MKMapItem alloc] initWithPlacemark:toPlacemark]; DirectionRouteUtils *utils = [DirectionRouteUtils sharedInstance];
[utils findDirectionsFrom:fromItem to:toItem handler:^(MKDirectionsResponse *response, NSError *error) {
if (error) { NSLog(@"error:%@", error);
}
else {
NSLog(@"%@", response.routes);
MKRoute *route = response.routes[0]; for(MKRoute *step in route.steps)
{
NSLog(@"Step: %@", ((MKRouteStep *)step).instructions);
} } }];
}
iOS MapKit导航及地理转码辅助类的更多相关文章
- iOS使用Zbar扫描二维码
iOS使用Zbar扫描二维码 标签(空格分隔):二维码扫描 iOS Zbar64位 正文: 首先下载一个支持64位系统的ZbarSDK的包,保存在了我的云盘里,地址:ZbarSDK 把文件拖到工程里面 ...
- iOS开发-定制多样式二维码
iOS开发-定制多样式二维码 二维码/条形码是按照某种特定的几何图形按一定规律在平台(一维/二维方向上)分布的黑白相间的图形纪录符号信息.使用若干个与二进制对应的几何形体来表示文字数值信息. 最常 ...
- ios 修改导航条返回按钮
ios 修改导航条返回按钮 方式一:使用系统的:可以更改系统的文字:以及通过设置导航条的颜色来达到预期的效果 UIBarButtonItem *backBtns = [[UIBarButtonItem ...
- IOS 改变导航栏返回按钮的标题
IOS 改变导航栏返回按钮的标题 下午又找到了一个新的方法 这个方法不错 暂时没有发现异常的地方. 新写的App中需要使用UINavigationController对各个页面进行导航,但由于第一 ...
- 一个功能齐全的IOS音乐播放器应用源码
该源码是在ios教程网拿过来的,一个不错的IOS音乐播放器应用源码,这个是我当时进公司时 我用了一晚上写的 图片都是在别的地方扒的,主要是歌词同步,及上一曲,下一曲,功能齐全了 ,大家可以学习一下吧 ...
- ios版弹珠游戏源码
这个是我们比较喜欢玩的一直小游戏的,ios版弹珠游戏源码,该游戏源码来着IOS教程网其他网友提供上传的,大家可以了解一下吧. nore_js_op> <ignore_js_op&g ...
- iOS原生CIFilter创建二维码
iOS原生CIFilter创建二维码 2016-05-31 未来C iOS原生CIFilter创建二维码 关于二维码生成,网上也是有很多,很早以前的第三方库大多数都是通过C++写,也是有的如zxing ...
- IOS版新闻客户端应用源码项目
IOS版新闻客户端应用源码,这个是一款简单的新闻客户端源码,该应用实现没采用任何第三方类库的 ,并且这个应用的UI做得很不错的,值得我们的参考和学习,希望大家可以更加完善这款新闻类的应用吧. 源码下载 ...
- iOS 指南针的制作 附带源码
iOS 指南针的制作 附带源码 代码下载地址: http://vdisk.weibo.com/s/HK4yE http://pan.baidu.com/share/link?shareid=7 ...
随机推荐
- 终端查询数据库sqlite(创建你自己,或者是coredata创建)那里的东西
首先需要知道数据库的路径,coredata一般都是在沙箱中创建Documents目录的.虽然他建立了自己看, sqlite3 #数据库路径# //进入数据库 .tables //查看数 ...
- LINQ之路系列文章导读
本系列文章将会分为3篇来进行阐述,如下: LINQ之路(1):LINQ基础 LINQ之路(2):LINQ to SQL本质 LINQ之路(3):LINQ扩展
- Cocos2dx-3.1.1 冒险01----> 文件夹结构、新项目project创建并执行
windows开发环境:window7.vs2012.python2.7.6 Cocos2d-x 3.1.1的完整文件夹例如以下:比起曾经的2.x的版本号来说分类更规范了 watermark/2/te ...
- 2014辽宁ACM省赛 Prime Factors
问题 L: Prime Factors 时间限制: 1 Sec 内存限制: 128 MB [提交][状态][论坛] 题目描写叙述 I'll give you a number , please te ...
- 【SSH2(实用文章)】--Struts2文件上传和下载的例子
回想一下,再上一篇文章Struts2实现机制,该步骤做一步一步来解决,这种决心不仅要理清再次Struts2用法.映射机制及其在深入分析.最后一个例子来介绍Struts2一种用法,这里将做一个有关文件上 ...
- 如何有效抓取SQL Server的BLOCKING信息
原文:如何有效抓取SQL Server的BLOCKING信息 转自:微软亚太区数据库技术支持组 官方博客 http://blogs.msdn.com/b/apgcdsd/archive/2011/12 ...
- Java Web系列:Spring Boot 基础 (转)
Spring Boot 项目(参考1) 提供了一个类似ASP.NET MVC的默认模板一样的标准样板,直接集成了一系列的组件并使用了默认的配置.使用Spring Boot 不会降低学习成本,甚至增加了 ...
- 定制Attribute
目录 Attribute是什么 自定义Attribute 一.Attribute是什么 将一些附加信息与制定目标相关联的方式.编译器在元数据中生成这些额外的信息.也叫做特性. 比如之前文章中提到的:枚 ...
- 泛泰A900 刷4.4中国民营TWRP2.7.1.1版本 支持自己主动识别移动版本号(世界上第一)
因本人手上的A900S已砖, 所以临时弄不了ROM了. 先上传之前已经弄好的刷4.4专用的新版TWRP recovery 2.7.1.1 这个版本号是我自己定义的,为差别之前公布的2.7.0.0版( ...
- [Elasticsearch] 部分匹配 (一) - 前缀查询
部分匹配(Partial Matching) 敏锐的读者可能已经发现到眼下为止,介绍的查询都是在整个词条层面进行操作的. 匹配的最小单元必须是一个词条.你仅仅能找到存在于倒排索引(Inverted I ...