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 ...
随机推荐
- win8.1 usb3 速度慢的解决方法
我买的金士顿u盘 驱动也装了,口业插对了 速度还是很慢 30m左右 解决方法 1 搞定了·,主板驱动问题.卸了主板驱动usb3.0的速度立马上100m/s,即使装了联想官方的驱动也会影响USB3.0的 ...
- c++日历v1.12版
////////////////////////////新增信息修改功能,未完善. #include<iostream> #include <string> #include& ...
- xml在此生活
小编尾随学习的步伐.今天小编简要概述xml在此生活,xml她的百度百科这一解释:可扩展标记语言 (ExtensibleMarkup Language, XML).用于标记电子文件使其具有结构性的标记语 ...
- expandableListView的divider该溶液显示在黑色
黑色是divider高度.如何让他成为透明的啊? 布局例如以下: <ExpandableListView android:layout_width="wrap_content&qu ...
- LeetCode——Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- OAuth做webapi认证
OAuth做webapi认证 看到园子里面有人写的OAuth,就想把自己实现的OAuth也分享一下,关于OAuth协议这里就不再赘述. 一.作为认证服务器,首先需要提供一个可以通过appid/apps ...
- 将android界面背景设置为黑色
屏幕背景设置为黑色的几种方式: 新建项目时候 第二次next之后(不同sdk版本号可能不同),Background Color项点击可选. 开公布局文件,选择视图查看 就是下边二个选项卡中的第一个(G ...
- android键盘锁定问题
android经常使用KeyguardLock解锁.但需要使用后打电话reenableKeyguard()锁定被解除.否则,会导致其他进程无法锁定屏幕,使用相同的WakeLock唤醒屏幕后还需要使用r ...
- SCM白色幼儿系列(十二) Proteus仿真软件简介
Proteus软件是英国Labcenter electronics公司出版的EDA工具软件.经常使用于单片机等数字电路仿真,分为ISIS和ARES两个程序,前者用于仿真,后者用于设计PCB.我们常使用 ...
- CCFadeOut ,CCFadeIn 不能使用的原因
CCFadeOut *action = CCFadeOut::create(0.5f); image->runAction(action); 截取部分代码.以上是我写游戏时候遇到的问题代码, ...