头文件:

#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导航及地理转码辅助类的更多相关文章

  1. iOS使用Zbar扫描二维码

    iOS使用Zbar扫描二维码 标签(空格分隔):二维码扫描 iOS Zbar64位 正文: 首先下载一个支持64位系统的ZbarSDK的包,保存在了我的云盘里,地址:ZbarSDK 把文件拖到工程里面 ...

  2. iOS开发-定制多样式二维码

    iOS开发-定制多样式二维码   二维码/条形码是按照某种特定的几何图形按一定规律在平台(一维/二维方向上)分布的黑白相间的图形纪录符号信息.使用若干个与二进制对应的几何形体来表示文字数值信息. 最常 ...

  3. ios 修改导航条返回按钮

    ios 修改导航条返回按钮 方式一:使用系统的:可以更改系统的文字:以及通过设置导航条的颜色来达到预期的效果 UIBarButtonItem *backBtns = [[UIBarButtonItem ...

  4. IOS 改变导航栏返回按钮的标题

    IOS 改变导航栏返回按钮的标题   下午又找到了一个新的方法 这个方法不错 暂时没有发现异常的地方. 新写的App中需要使用UINavigationController对各个页面进行导航,但由于第一 ...

  5. 一个功能齐全的IOS音乐播放器应用源码

    该源码是在ios教程网拿过来的,一个不错的IOS音乐播放器应用源码,这个是我当时进公司时 我用了一晚上写的  图片都是在别的地方扒的,主要是歌词同步,及上一曲,下一曲,功能齐全了 ,大家可以学习一下吧 ...

  6. ios版弹珠游戏源码

    这个是我们比较喜欢玩的一直小游戏的,ios版弹珠游戏源码,该游戏源码来着IOS教程网其他网友提供上传的,大家可以了解一下吧. nore_js_op>     <ignore_js_op&g ...

  7. iOS原生CIFilter创建二维码

    iOS原生CIFilter创建二维码 2016-05-31 未来C iOS原生CIFilter创建二维码 关于二维码生成,网上也是有很多,很早以前的第三方库大多数都是通过C++写,也是有的如zxing ...

  8. IOS版新闻客户端应用源码项目

    IOS版新闻客户端应用源码,这个是一款简单的新闻客户端源码,该应用实现没采用任何第三方类库的 ,并且这个应用的UI做得很不错的,值得我们的参考和学习,希望大家可以更加完善这款新闻类的应用吧. 源码下载 ...

  9. iOS 指南针的制作 附带源码

    iOS  指南针的制作  附带源码 代码下载地址: http://vdisk.weibo.com/s/HK4yE   http://pan.baidu.com/share/link?shareid=7 ...

随机推荐

  1. IIS安装asp组件:JMail 邮件收发组件

    JMail简介 jmail是一种服务器端的邮件发送组件,和个人用的客户端邮件软件不一样的.jmail是在服务器上给程序用来发邮件用的,除了软件编程人员,其他人一般平常用不上. jmail是一个第三方邮 ...

  2. 马云收购UC你,至于到底是谁宣战

    近日,阿里巴巴官方正式宣布,UC优视全资融入阿里巴巴集团,并组建阿里UC移动事业群. 据阿里方面表示,整个交易对UC的估值远超百度对91无线的估值,外界推測估值近50美元.是中国互联网最大一笔交易.业 ...

  3. python字典构造函数dict(mapping)解析

    Python字典的构造函数有三个,dict().dict(**args).dict(mapping),当中第一个.第二个构造函数比較好理解也比較easy使用, 而dict(mapping)这个构造函数 ...

  4. Java Web项目结构

    Java Web项目结构(一般) 1.Java src 2.JRE System Library 3.Java EE 6 Libraries 4.Web App Libraries 5.WebRoot ...

  5. [ACM] HDU 2295 Radar (二分法+DLX 重复覆盖)

    Radar Problem Description N cities of the Java Kingdom need to be covered by radars for being in a s ...

  6. 于XAML导入命名空间的代码

    例如,下面的代码到指定的命名空间.不仅导入的命名空间,并且还为指定的命名空间前缀local.当然,你也可以指定一个前缀为另一个名称,这可以定义.导入后,市民可以在命名当前空间XAML使用代码.例如,在 ...

  7. github中origin和upstream的区别(转)

    Fork,本身并不是git工具中的一个命令,也不是对git的扩展,它是在GitHub上的概念,是另一种clone方式——在服务器端的clone.而我们通常意义上的clone,是将远程repo 复制一份 ...

  8. 新秀翻译(两)——使用Java通用配置模板方法模式

    假设你发现你已经非常重码,你可能会考虑使用模板的方法来消除easy重复错误代码.下面是一个示例:以下两类,他完成了几乎相同的功能: 实例化并初始化一个Reader来读取CSV文件. 读取每一行并解析: ...

  9. 编程算法基地-2.1利用字符串API

    2.1利用字符串API 字符串是Java类型最常用.并且是复合类型 串非常经常用于,其最佳API熟悉文档. 推断串中有没有反复的字符 String s ="abcdebxyz"; ...

  10. 创建Windows类别

    Windows在表单.控制.对话框基本上形成.Windows类是Windows形式的类型,可处理叙述性说明. 在Windows提前有很多定义Windows类别,但它可以很容易地创建自己的Windows ...