iOS原生地图开发进阶——使用导航和附近兴趣点检索
iOS中的mapKit框架对国际化的支持非常出色。在前些篇博客中,对这个地图框架的基础用法和标注与覆盖物的添加进行了详细的介绍,这篇博客将介绍两个更加实用的功能的开发:线路导航与兴趣点搜索。前几篇博客的链接如下:
地图基础用法详解:http://my.oschina.net/u/2340880/blog/415360。
添加大头针与自定义标注:http://my.oschina.net/u/2340880/blog/415441。
添加地图覆盖物:http://my.oschina.net/u/2340880/blog/415611。
一、线路导航
1、从几个类的关系说起
(1)MKPlacemark
一个地点信息类,如下:
@interface MKPlacemark : CLPlacemark <MKAnnotation>
//初始化方法,通过给定一个经纬度和地点信息字典
- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate
addressDictionary:(NSDictionary *)addressDictionary;
//国家编码
@property (nonatomic, readonly) NSString *countryCode;
@end
(2)MKMapItem
地点节点类,包含此节点的许多地点信息,如下:
@interface MKMapItem : NSObject
//当前节点的地点信息对象
@property (nonatomic, readonly) MKPlacemark *placemark;
//是否是当前位置
@property (nonatomic, readonly) BOOL isCurrentLocation;
//节点名称
@property (nonatomic, copy) NSString *name;
//电话号码
@property (nonatomic, copy) NSString *phoneNumber;
//网址
@property (nonatomic, strong) NSURL *url;
//将当前位置创建为节点
+ (MKMapItem *)mapItemForCurrentLocation;
//由一个位置信息创建节点
- (instancetype)initWithPlacemark:(MKPlacemark *)placemark;
@end
(3)MKDirectionsRequest
导航请求类
@interface MKDirectionsRequest : NSObject
//起点节点
- (MKMapItem *)source NS_AVAILABLE(10_9, 6_0);
- (void)setSource:(MKMapItem *)source NS_AVAILABLE(10_9, 7_0);
//目的地节点
- (MKMapItem *)destination NS_AVAILABLE(10_9, 6_0);
- (void)setDestination:(MKMapItem *)destination NS_AVAILABLE(10_9, 7_0);
@end
这个类还有一些扩展的设置属性:
@property (nonatomic) MKDirectionsTransportType transportType;
设置路线检索类型,枚举如下:
typedef NS_OPTIONS(NSUInteger, MKDirectionsTransportType) {
MKDirectionsTransportTypeAutomobile = 1 << 0,//适合驾车时导航
MKDirectionsTransportTypeWalking = 1 << 1,//适合步行时导航
MKDirectionsTransportTypeAny = 0x0FFFFFFF//任何情况
};
@property (nonatomic) BOOL requestsAlternateRoutes;
设置是否搜索多条线路
@property (nonatomic, copy) NSDate *departureDate;
设置出发日期
@property (nonatomic, copy) NSDate *arrivalDate;
设置到达日期
(4)MKDirections
从apple服务器获取数据的连接类
@interface MKDirections : NSObject
//初始化方法
- (instancetype)initWithRequest:(MKDirectionsRequest *)request NS_DESIGNATED_INITIALIZER;
//开始计算线路信息
- (void)calculateDirectionsWithCompletionHandler:(MKDirectionsHandler)completionHandler;
//开始计算时间信息
- (void)calculateETAWithCompletionHandler:(MKETAHandler)completionHandler;
//取消
- (void)cancel;
//是否正在计算
@property (nonatomic, readonly, getter=isCalculating) BOOL calculating;
@end
(5)MKDirectionsResponse
线路信息结果类
@interface MKDirectionsResponse : NSObject
@property (nonatomic, readonly) MKMapItem *source;//起点
@property (nonatomic, readonly) MKMapItem *destination;//终点
@property (nonatomic, readonly) NSArray *routes; //线路规划数组
@end
(6)MKETResponse
时间信息结果类
@interface MKETAResponse : NSObject
@property (nonatomic, readonly) MKMapItem *source;//起点
@property (nonatomic, readonly) MKMapItem *destination;//终点
@property (nonatomic, readonly) NSTimeInterval expectedTravelTime;//耗时
@end
(7)MKRoute
线路信息类,导航的线路结果是这个类型的对象
@interface MKRoute : NSObject
@property (nonatomic, readonly) NSString *name; //线路名称
@property (nonatomic, readonly) NSArray *advisoryNotices; //注意事项
@property (nonatomic, readonly) CLLocationDistance distance; //距离
@property (nonatomic, readonly) NSTimeInterval expectedTravelTime;//耗时
@property (nonatomic, readonly) MKDirectionsTransportType transportType; //检索的类型
@property (nonatomic, readonly) MKPolyline *polyline; // 线路覆盖物
@property (nonatomic, readonly) NSArray *steps; // 线路详情数组
@end
(8)MKRouteStep
线路详情信息类,线路中每一步的信息都是这个类的对象
@interface MKRouteStep : NSObject
@property (nonatomic, readonly) NSString *instructions; // 节点信息
@property (nonatomic, readonly) NSString *notice; // 注意事项
@property (nonatomic, readonly) MKPolyline *polyline; //线路覆盖物
@property (nonatomic, readonly) CLLocationDistance distance; // 距离
@property (nonatomic, readonly) MKDirectionsTransportType transportType; // 导航类型
@end