头文件:

#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. Connect2015 简要整理

    2015 简要整理 去年 Connect(); 2014 Visual Studio Contact(); 直播笔记 对于我个人来说,今年 Connect(); 的三个重要发布: ASP.NET 5 ...

  2. C# 读取IE缓存文件(2)

    private void button1_Click(object sender, EventArgs e) { , nBufSize; IntPtr buf; INTERNET_CACHE_ENTR ...

  3. Linking pronunciation in English

    1.constant+vowel stand up give up get up 2.vowel+vowel 2.1 i:/i/ei/ai/oi [j] stay up carry it 2.2 u: ...

  4. Visual Studio 2015使用EF6的CodeFirstFromDB模式操作Sqlite数据库时Provider问题

    传送门 什么是CodeFristFromDB 问题:查询数据是遇到 “/”应用程序中的服务器错误. No Entity Framework provider found for the ADO.NET ...

  5. BZOJ 3589 动态树 树链拆分+纳入和排除定理

    标题效果:鉴于一棵树.每个节点有一个右值,所有节点正确启动值他们是0.有两种操作模式,0 x y代表x右所有点的子树的根值添加y. 1 k a1 b1 a2 b2 --ak bk代表质疑. 共同拥有者 ...

  6. gem 安装nokigiri

    在mac上安装nokogiri的时候各种报错,终于安装成功一次,备份命令. ➜ ~ sudo gem install nokogiri -- --use-system-libraries --with ...

  7. SQL Server安全性专题一:简介

    原文:SQL Server安全性专题一:简介 一. 安全威胁与法则 1. 安全定义 2. 安全威胁 3. 安全法则 安全定义: 在SQLServer环境中,安全性可以认为是[数据保护].包括:  数 ...

  8. python2编码总结(转)

    以下依次列出python2常遇到的几个问题及讲解. # -*- coding:utf-8 -*- python2默认以ASCII编码,但是在实际编码过程中,我们会用到很多中文,为了不使包含中文的程序报 ...

  9. Data URI(转)

    Data URL 早在 1995 年就被提出,那个时候有很多个版本的 Data URL Schema 定义陆续出现在 VRML 之中,随后不久,其中的一个版本被提上了议案——将它做个一个嵌入式的资源放 ...

  10. linux虚拟机网络配制方法及遇到问题的解决方法

    linux虚拟机网络问题 刚安装一个vmware虚拟机.并在上面安装了一个redhat linux操作系统. 安装完后配制了下网络. 首先观察windows上的网络配制. ip:192.168.1.1 ...