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 ...
随机推荐
- LeetCode Solutions : Reorder List
→-→Ln-1→Ln, reorder it to: L→Ln-2→- You must do this in-place without altering the nodes' values. Fo ...
- 大数据的胖哥的方式(9)- 金融业数据仓库的逻辑模型FS-LDM
介绍: 大数据是不是海市蜃楼,来自小橡子只是意淫奥克斯,大数据的发展,而且要从头开始,基于大数据建设国家.项目-level数据中心行业将越来越多,大数据仅供技术,而非溶液,临数据组织模式,数据逻辑模式 ...
- 第三篇——第二部分——第六文 监控SQL Server镜像
原文:第三篇--第二部分--第六文 监控SQL Server镜像 原文出处:http://blog.csdn.net/dba_huangzj/article/details/26846203 要优化, ...
- (初稿)SQL Server 复制(Replication)系列(2)——事务复制搭建
原文:(初稿)SQL Server 复制(Replication)系列(2)--事务复制搭建 本文演示如何搭建最基本的事务复制. 环境准备: 虚拟机2台: 服务器名分别为RepA和RepB,RepA为 ...
- 在SQL Server Management Studio中可以运行作业但是用T-SQL运行则失败
原文:在SQL Server Management Studio中可以运行作业但是用T-SQL运行则失败 问题: 在SQL Server Management Studio中可以运行作业但是用T-SQ ...
- 如何识别SQL Server中的IO瓶颈
原文:如何识别SQL Server中的IO瓶颈 原文出自: http://www.mssqltips.com/sqlservertip/2329/how-to-identify-io-bottlene ...
- 使用一个T-SQL语句批量查询数据表占用空间及其行数
原文:使用一个T-SQL语句批量查询数据表占用空间及其行数 要找到数据库中数据表占用的空间和存在的行数.可以使用sp_spaceused搭配数据表的名称.就可以产生该表耗用的空间和现有行数. 如: U ...
- 2014ACM上海邀请赛A解释称号
#include <cstdio> #include <cstring> #include <iostream> using namespace std; cons ...
- ExtJS学习笔记:定义extjs类别
类的定义 Ext.define('Cookbook.Vehicle', { Manufacturer: 'Aston Martin', Model: 'Vanquish', getDetails: f ...
- 开源GUI-Microwindows之程序入口分析
**************************************************************************************************** ...