地图的相关使用(定位,地理编码,导航)
2015-09-15 16:55
211 查看
概述
在移动互联网时代,很多社交类,团购类app都引入了地图,地图能解决用户的很多生活琐事,比如:
导航:去任意陌生的地方,汽车导航等
周边:找餐馆、找酒店、找银行、找电影院
总之,目前地图和定位功能已经大量引入到应用开发中。
在上述应用中,都用到了地图和定位功能,在iOS开发中,要想加入这2大功能,必须基于2个框架进行开发, Map Kit :用于地图展示 , Core Location :用于地理定位.
下面介绍地图经常使用的类和方法.
定位,地理编码(反编码)
CLLocationManager
CLLocationManager的常用操作:
开始用户定位
停止用户定位
当调用了startUpdatingLocation方法后,就开始不断地定位用户的位置,中途会频繁地调用代理的下面方法
locations参数里面装着CLLocation对象
CLLocation
CLLocation用来表示某个位置的地理信息,比如经纬度、海拔等等
用- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location方法可以计算2个位置之间的距离
用户隐私
从iOS 8开始,用户定位分两种情况:
总是使用用户位置:NSLocationAlwaysUsageDescription
使用应用时定位:NSLocationWhenInUseDescription
当想访问用户的隐私信息时,系统会自动弹出一个对话框让用户授权,需要在info.plist中设置
为了严谨起见,最好在使用定位功能之前判断当前应用的定位功能是否可用
CLLocationManager有个类方法可以判断当前应用的定位功能是否可用
iOS8需要先请求用户定位方式
第一种方式:
第二种方式:
CLLocationCoordinate2D
CLLocationCoordinate2D是一个用来表示经纬度的结构体,定义如下
一般用CLLocationCoordinate2DMake函数来创建CLLocationCoordinate2D
CLGeocoder (地理编码,反地理编码)
使用CLGeocoder可以完成“地理编码”和“反地理编码”
地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
反地理编码:根据给定的经纬度,获得具体的位置信息
地理编码方法:
反地理编码方法:
CLGeocodeCompletionHandler
当地理反地理编码完成时,就会调用CLGeocodeCompletionHandler
typedef void (^CLGeocodeCompletionHandler)(NSArray *placemarks, NSError *error);
这个block传递2个参数
error :当编码出错时(比如编码不出具体的信息)有值
placemarks :里面装着CLPlacemark对象
地理编码和反地理编码,在开发中是比较常用的,下面是一个简单的应用:
首先,使用storyBoard搭建界面,如下图:
代码实现:
实现效果图:
CLPlacemark
CLPlacemark的字面意思是地标,封装详细的地址位置信息
addressDictionary字典里又包括如下信息:
关系概览图
地图相关
在iOS中进行地图开发主要有两种方式,一种是直接利用MapKit框架进行地图开发,利用这种方式可以对地图进行精准的控制;另一种方式是直接调用苹果官方自带的地图应用,主要用于一些简单的地图应用(例如:进行导航覆盖物填充等),无法进行精确的控制。
下面介绍MapKit中地图展示控件MKMapView的的一些常用属性和方法:
跟踪显示用户的位置
设置MKMapView的userTrackingMode属性可以跟踪显示用户的当前位置
MKUserTrackingModeNone :不跟踪用户的位置
MKUserTrackingModeFollow :跟踪并在地图上显示用户的当前位置
MKUserTrackingModeFollowWithHeading :跟踪并在地图上显示用户的当前位置,地图会跟随用户的前进方向进行旋转
地图类型
可以通过设置MKMapView的mapViewType设置地图类型
MKMapTypeStandard :普通地图(默认)
MKMapTypeSatellite :卫星云图
MKMapTypeHybrid :普通地图覆盖于卫星云图之上
设置地图的显示
通过MKMapView的下列方法,可以设置地图显示的位置和区域
设置地图的中心点位置
设置地图的显示区域
MKCoordinateRegion (当前用户位置的区域)
MKCoordinateRegion是一个用来表示区域的结构体,定义如下
MKCoordinateSpan的定义
点击某一按钮回到当前区域:
MKMapView的代理方法
MKMapView可以设置一个代理对象,用来监听地图的相关行为
常见的代理方法有:
iOS8之后,定位到用户的位置,会自动设置用户的位置为中心点和显示区域:
一个位置更改默认只会调用一次,不断监测用户的当前位置,每次调用都会把用户的最新位置(userLocation参数)传进来
地图的显示区域即将发生改变的时候调用:
地图的显示区域已经发生改变的时候调用:
大头针的相关操作
MKUserLocation(系统)
MKUserLocation其实是个大头针模型(系统大头针),包括以下属性:
大头针的基本操作
添加大头针示例代码:
根据在地图上点击的点,进行添加大头针的方法:
自定义大头针
自定义大头针步骤:
1> 设置MKMapView的代理
2> 实现下面的代理方法,返回大头针控件
3> 根据传进来的(id )annotation参数创建并返回对应的大头针控件
代理方法的使用注意:
如果返回nil,显示出来的大头针就采取系统的默认样式 , 标识用户位置的蓝色发光圆点,它也是一个大头针,当显示这个大头针时,也会调用代理方法
因此,需要在代理方法中分清楚(id )annotation参数代表自定义的大头针还是蓝色发光圆点
MKAnnotationView
地图上的大头针控件是MKAnnotationView , MKAnnotationView的属性:
MKPointAnnotationView
MKPinAnnotationView是MKAnnotationView的子类 , MKPinAnnotationView比MKAnnotationView多了2个属性
下面是自定义大头针的地图使用示例代码:
KFAnnotion.h
KFAnnotationView.h
KFAnnotationView.m
LocationViewController.m
实现效果图:
定位在当前位置
点击添加大头针,会显示两颗大头针
导航设置(使用苹果自带的高德地图) 和画出两个位置的路线图
有时候我们需要在地图上进行标注、收藏或者浏览相关路线等,这时我们希望能够打开地图程序进行相应的功能操作。苹果为我们提供了相关的API:MKMapItem。
MKMapItem的特点如下:是OC API , 可以通过一个或者多个pins来打开地图 , 直接转至某个地方 , 定制地图的显示。
MKMapItem的相关属性和方法:
launchOptions的可选项:
MKLaunchOptionsDirectionsModeKey 路线模式,常量
MKLaunchOptionsDirectionsModeDriving 驾车模式
MKLaunchOptionsDirectionsModeWalking 步行模式
MKLaunchOptionsMapTypeKey 地图类型,枚举
MKMapTypeStandard :标准模式
MKMapTypeSatellite :卫星模式
MKMapTypeHybrid :混合模式
MKLaunchOptionsShowsTrafficKey 是否显示交通状况,布尔型
MKLaunchOptionsCameraKey 3D地图效果,MKMapCamera类型
注意:此属性从iOS7及以后可用,前面的属性从iOS6开始可用
MKPlacemark:类似于CLPlacemark,只是它在MapKit框架中,可以根据CLPlacemark创建MKPlacemark,它存储了经纬度信息。
MKDirectionsRequest
MKDirectionsRequest 有一些其他控制返回路线信息的属性,如下:
1、departureDate 和arrivalDate。设置这些值,由于旅行时间的限制,将优化返回的路线,例如,会考虑到标准的路况信息。
2、TransportType。目前苹果通过枚举值MKDirectionsTransportTypeAutomobile 或者MKDirectionsTransportTypeWalking提供步行或者驾车方式。默认值是MKDirectionsTransportTypeAny。
3、RequestsAlternateRoutes。如果路由服务器可以找出多条合理的路线,设置YES将会返回所有路线。否则,只返回一条路线。
现在我们已经有了一个可用的请求,可以发送去请求路线。需要使用MKDirections 类,它有一个用MKDirectionsRequest对象构造的函数:
MKDirections 类两个方法:
两个方法都是异步的,并有一个completion handling块。MKDirections 对象也有一个取消方法,提供给当前任何正在运行的请求。还有calculating属性,如果当前有个请求正在执行,返回为true。单个的MKDirections对象一次只能运行一个请求,额外去请求将会失败。如果你想运行多个并发的请求,你需要创建多个MKDirections对象。但注意,请求太多的话可能会导致苹果服务器因为节流返回错误。
MKDirectionsResponse(方向指南响应)
从苹果服务器回送的对象是MKDirectionsResponse,还有源和目的地,包括MKRoute对象数组。注意,这个数组只有一个对象除非我们设置setrequestsAlternateRoutes为YES。
MKRoute 对象,如它的名字,代表用户选择的两点之间的路线。它包含一些关于路线信息的属性:
渲染polyline
我们已经收到路线的polyline,我们想把它体现到地图上。IOS7之后改变了地图渲染的方法,通过MKOverlayRenderer类。如果我们想做自定义形状或者非标准渲染技术,可以定义一个子类。但是,许多叠加渲染技术都是为标准用例使用的。
我们想渲染一个polyline,可以使用对象MKPolylineRenderer。我们创建MKPolylineRenderer对象,它是MKOverlayRenderer的子类,目的是为了绘制polyline叠加层。我们设置了一些简单的属性(strokeColor 和 lineWidth),以便于可以看到叠加层,然后返回新的对象。
MKOverlayPathRenderer的一些常用属性:
当叠加层添加到地图时调用mapView的代理方法:
下面是示例代码:
NavigateViewController.m
实现结果:
使用系统导航:
两个位置的路线图:
在移动互联网时代,很多社交类,团购类app都引入了地图,地图能解决用户的很多生活琐事,比如:
导航:去任意陌生的地方,汽车导航等
周边:找餐馆、找酒店、找银行、找电影院
总之,目前地图和定位功能已经大量引入到应用开发中。
在上述应用中,都用到了地图和定位功能,在iOS开发中,要想加入这2大功能,必须基于2个框架进行开发, Map Kit :用于地图展示 , Core Location :用于地理定位.
下面介绍地图经常使用的类和方法.
定位,地理编码(反编码)
CLLocationManager
CLLocationManager的常用操作:
开始用户定位
[code]- (void)startUpdatingLocation;
停止用户定位
[code]- (void) stopUpdatingLocation;
当调用了startUpdatingLocation方法后,就开始不断地定位用户的位置,中途会频繁地调用代理的下面方法
[code]- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
locations参数里面装着CLLocation对象
CLLocation
CLLocation用来表示某个位置的地理信息,比如经纬度、海拔等等
[code]@property(readonly, nonatomic) CLLocationCoordinate2D coordinate; // 经纬度 @property(readonly, nonatomic) CLLocationDistance altitude; // 海拔 @property(readonly, nonatomic) CLLocationDirection course; // 路线,航向(取值范围是0.0° ~ 359.9°,0.0°代表真北方向) @property(readonly, nonatomic) CLLocationSpeed speed; // 行走速度(单位是m/s) @property(assign, nonatomic) CLLocationDistance distanceFilter; // 每隔多少米定位一次 @property(assign, nonatomic) CLLocationAccuracy desiredAccuracy; // 定位精确度(越精确就越耗电)
用- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location方法可以计算2个位置之间的距离
[code]//第一个坐标 CLLocation *current=[[CLLocation alloc] initWithLatitude:32.178722 longitude:119.508619]; //第二个坐标 CLLocation *before=[[CLLocation alloc] initWithLatitude:32.206340 longitude:119.425600]; // 计算距离 CLLocationDistance meters=[current distanceFromLocation:before];
用户隐私
从iOS 8开始,用户定位分两种情况:
总是使用用户位置:NSLocationAlwaysUsageDescription
使用应用时定位:NSLocationWhenInUseDescription
当想访问用户的隐私信息时,系统会自动弹出一个对话框让用户授权,需要在info.plist中设置
为了严谨起见,最好在使用定位功能之前判断当前应用的定位功能是否可用
CLLocationManager有个类方法可以判断当前应用的定位功能是否可用
[code]+ (BOOL)locationServicesEnabled;
iOS8需要先请求用户定位方式
第一种方式:
[code]if ([self.mgr respondsToSelector:@selector(requestAlwaysAuthorization)]) { // 获取授权 [self.mgr requestAlwaysAuthorization]; }
第二种方式:
[code]if ([CLLocationManager locationServicesEnabled]){ if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { // 获取授权 [self.locationManager requestAlwaysAuthorization]; } }
CLLocationCoordinate2D
CLLocationCoordinate2D是一个用来表示经纬度的结构体,定义如下
[code]typedef struct { CLLocationDegrees latitude; // 纬度 CLLocationDegrees longitude; // 经度 } CLLocationCoordinate2D;
一般用CLLocationCoordinate2DMake函数来创建CLLocationCoordinate2D
CLGeocoder (地理编码,反地理编码)
使用CLGeocoder可以完成“地理编码”和“反地理编码”
地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
反地理编码:根据给定的经纬度,获得具体的位置信息
地理编码方法:
[code]- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;
反地理编码方法:
[code]- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
CLGeocodeCompletionHandler
当地理反地理编码完成时,就会调用CLGeocodeCompletionHandler
typedef void (^CLGeocodeCompletionHandler)(NSArray *placemarks, NSError *error);
这个block传递2个参数
error :当编码出错时(比如编码不出具体的信息)有值
placemarks :里面装着CLPlacemark对象
地理编码和反地理编码,在开发中是比较常用的,下面是一个简单的应用:
首先,使用storyBoard搭建界面,如下图:
代码实现:
[code]/** 北京: 经度:116.28 纬度:39.54 重庆市: 北纬29.35 东经106.33 */ #import "ViewController.h" #import <MapKit/MapKit.h> @interface ViewController () // 反地理编码 @property (weak, nonatomic) IBOutlet UITextField *longitudeField; @property (weak, nonatomic) IBOutlet UITextField *latitudeField; @property (weak, nonatomic) IBOutlet UILabel *reverseGeodeLable; // 地理编码 @property (weak, nonatomic) IBOutlet UITextField *addressNameField; @property (weak, nonatomic) IBOutlet UITextField *geocodeLongitudeField; @property (weak, nonatomic) IBOutlet UITextField *geocodeLatitudeField; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } // 反地理编码 : 根据经纬度,获得具体位置信息 - (IBAction)reverGeocode:(UIButton *)sender { // 取出经纬度 NSString *longitude = self.longitudeField.text; NSString *latitude = self.latitudeField.text; if (latitude.length == 0 || longitude.length == 0) return; CLGeocoder *geocoder = [[CLGeocoder alloc] init]; // 根据经纬度获取位置信息 CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude.floatValue longitude:longitude.floatValue]; // 反编码 [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { if (!error) { // 取得第一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址 MKPlacemark *placemark = [placemarks firstObject]; // 位置 CLLocation *location = placemark.location; // 区域 CLRegion *regin = placemark.region; self.reverseGeodeLable.text = placemark.locality; // 如果是取出城市的话,需要判断locality属性是否有值(直辖市时,该属性为空) // self.reverseGeodeLable.text = placemark.locality ? placemark.locality : placemark.administrativeArea; NSLog(@"%@ -------- %@",location,regin); } }]; } // 地理编码 : 根据地名获取经纬度 - (IBAction)geocode:(UIButton *)sender { // 地址 NSString *address = self.addressNameField.text; if (address.length == 0) return; CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) { if (!error) { // 取得第一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址 MKPlacemark *placemark = [placemarks firstObject]; NSLog(@"%@",placemark.name); // 位置 CLLocation *location = placemark.location; // 取出经纬度 CLLocationCoordinate2D coordinate = location.coordinate; self.geocodeLatitudeField.text = [NSString stringWithFormat:@"%.2f", coordinate.latitude]; self.geocodeLongitudeField.text = [NSString stringWithFormat:@"%.2f", coordinate.longitude]; } }]; } // 点击空白回收键盘 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } @end
实现效果图:
CLPlacemark
CLPlacemark的字面意思是地标,封装详细的地址位置信息
[code]@property (nonatomic, readonly) CLLocation *location; // 地理位置 @property (nonatomic, readonly) CLRegion *region; // 区域 @property (nonatomic, readonly) NSDictionary *addressDictionary; // 详细的地址信息 @property (nonatomic, readonly) NSString *name; // 地址名称 @property (nonatomic, readonly) NSString *locality; // 城市
addressDictionary字典里又包括如下信息:
[code]name : 地名 thoroughfare : 街道 ubThoroughfare : 街道相关信息,例如门牌等 locality : 城市 subLocality : 城市相关信息,例如标志性建筑 administrativeArea : 直辖市 subAdministrativeArea : 其他行政区域信息 postalCode : 邮编 ISOcountryCode : 国家编码 country; : 国家 inlandWater : 水源、湖泊 ocean; : 海洋 areasOfInterest : 关联的或利益相关的地标
关系概览图
地图相关
在iOS中进行地图开发主要有两种方式,一种是直接利用MapKit框架进行地图开发,利用这种方式可以对地图进行精准的控制;另一种方式是直接调用苹果官方自带的地图应用,主要用于一些简单的地图应用(例如:进行导航覆盖物填充等),无法进行精确的控制。
下面介绍MapKit中地图展示控件MKMapView的的一些常用属性和方法:
跟踪显示用户的位置
设置MKMapView的userTrackingMode属性可以跟踪显示用户的当前位置
MKUserTrackingModeNone :不跟踪用户的位置
MKUserTrackingModeFollow :跟踪并在地图上显示用户的当前位置
MKUserTrackingModeFollowWithHeading :跟踪并在地图上显示用户的当前位置,地图会跟随用户的前进方向进行旋转
地图类型
可以通过设置MKMapView的mapViewType设置地图类型
MKMapTypeStandard :普通地图(默认)
MKMapTypeSatellite :卫星云图
MKMapTypeHybrid :普通地图覆盖于卫星云图之上
设置地图的显示
通过MKMapView的下列方法,可以设置地图显示的位置和区域
设置地图的中心点位置
[code]@property (nonatomic) CLLocationCoordinate2D centerCoordinate; - (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated;
设置地图的显示区域
[code]@property (nonatomic) MKCoordinateRegion region; - (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated;
MKCoordinateRegion (当前用户位置的区域)
MKCoordinateRegion是一个用来表示区域的结构体,定义如下
[code]typedef struct { CLLocationCoordinate2D center; // 区域的中心点位置 MKCoordinateSpan span; // 区域的跨度 } MKCoordinateRegion;
MKCoordinateSpan的定义
[code]typedef struct { CLLocationDegrees latitudeDelta; // 纬度跨度 CLLocationDegrees longitudeDelta; // 经度跨度 } MKCoordinateSpan;
点击某一按钮回到当前区域:
[code]#pragma mark - 点击回到当前位置 - (IBAction)LocateAction:(UIButton *)sender { // 获得经纬度 CLLocationCoordinate2D coordinate = self.mapView.userLocation.location.coordinate; // 获取区域跨度 MKCoordinateSpan span = MKCoordinateSpanMake(0.052996, 0.039880); // 返回当前区域 MKCoordinateRegion regin = MKCoordinateRegionMake(coordinate, span); [self.mapView setRegion:regin animated:YES]; }
MKMapView的代理方法
MKMapView可以设置一个代理对象,用来监听地图的相关行为
常见的代理方法有:
iOS8之后,定位到用户的位置,会自动设置用户的位置为中心点和显示区域:
[code]- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation;
一个位置更改默认只会调用一次,不断监测用户的当前位置,每次调用都会把用户的最新位置(userLocation参数)传进来
地图的显示区域即将发生改变的时候调用:
[code](void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated;
地图的显示区域已经发生改变的时候调用:
[code](void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;
大头针的相关操作
MKUserLocation(系统)
MKUserLocation其实是个大头针模型(系统大头针),包括以下属性:
[code]@property (nonatomic, copy) NSString *title; // 显示在大头针上的标题 @property (nonatomic, copy) NSString *subtitle; // 显示在大头针上的子标题 @property (readonly, nonatomic) CLLocation *location; // 地理位置信息(大头针钉在什么地方?)
大头针的基本操作
[code]// 添加一个大头针 - (void)addAnnotation:(id <MKAnnotation>)annotation; // 添加多个大头针 - (void)addAnnotations:(NSArray *)annotations; // 移除一个大头针 - (void)removeAnnotation:(id <MKAnnotation>)annotation; // 移除多个大头针 - (void)removeAnnotations:(NSArray *)annotations; (id <MKAnnotation>)annotation参数是什么东西? 大头针模型对象:用来封装大头针的数据,比如大头针的位置、标题、子标题等数据
添加大头针示例代码:
[code]MKPointAnnotation *point = [[MKPointAnnotation alloc]init]; point.title = @"帝都"; point.subtitle = @"北京"; anno.coordinate = CLLocationCoordinate2DMake(40, 116); [self.mapView addAnnotation:anno];
根据在地图上点击的点,进行添加大头针的方法:
[code]- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 1.获取点击的点 CGPoint point = [[touches anyObject] locationInView:self.view]; // 2.将该点转换成MapView上的经纬度 CLLocationCoordinate2D coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView]; // 3.添加大头针 ……… }
自定义大头针
自定义大头针步骤:
1> 设置MKMapView的代理
2> 实现下面的代理方法,返回大头针控件
[code]- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
3> 根据传进来的(id )annotation参数创建并返回对应的大头针控件
代理方法的使用注意:
如果返回nil,显示出来的大头针就采取系统的默认样式 , 标识用户位置的蓝色发光圆点,它也是一个大头针,当显示这个大头针时,也会调用代理方法
因此,需要在代理方法中分清楚(id )annotation参数代表自定义的大头针还是蓝色发光圆点
MKAnnotationView
地图上的大头针控件是MKAnnotationView , MKAnnotationView的属性:
[code]@property (nonatomic, strong) id <MKAnnotation> annotation; // 大头针模型 @property (nonatomic, strong) UIImage *image; // 显示的图片 @property (nonatomic) BOOL canShowCallout; // 是否显示标注 @property (nonatomic) CGPoint calloutOffset; // 标注的偏移量 @property (strong, nonatomic) UIView *rightCalloutAccessoryView; // 标注右边显示什么控件 @property (strong, nonatomic) UIView *leftCalloutAccessoryView; // 标注左边显示什么控件
MKPointAnnotationView
MKPinAnnotationView是MKAnnotationView的子类 , MKPinAnnotationView比MKAnnotationView多了2个属性
[code]@property (nonatomic) MKPinAnnotationColor pinColor; // 大头针颜色 @property (nonatomic) BOOL animatesDrop; // 大头针第一次显示时是否从天而降
下面是自定义大头针的地图使用示例代码:
KFAnnotion.h
[code]#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface KFAnnotion : NSObject <MKAnnotation> @property (nonatomic, assign) CLLocationCoordinate2D coordinate; @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *subtitle; @property (copy, nonatomic) NSString *icon; @end
KFAnnotationView.h
[code]#import <MapKit/MapKit.h> @interface KFAnnotationView : MKAnnotationView // 类方法创建KFAnnotationView + (instancetype)annotationViewWithMapView:(MKMapView *)mapView; @end
KFAnnotationView.m
[code]#import "KFAnnotationView.h" #import "KFAnnotion.h" @implementation KFAnnotationView + (instancetype)annotationViewWithMapView:(MKMapView *)mapView { static NSString *indentifier = @"annotation"; KFAnnotationView *annotationView = (KFAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:indentifier]; // 从缓冲池中取出 if (!annotationView) { annotationView = [[KFAnnotationView alloc]initWithAnnotation:nil reuseIdentifier:indentifier]; // 可以点击交互 annotationView.canShowCallout = YES; // 设置辅助视图 annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; } return annotationView; } - (void)setAnnotation:(id<MKAnnotation>)annotation { [super setAnnotation:annotation]; KFAnnotion *KFannotation = annotation; self.image = [UIImage imageNamed:KFannotation.icon]; }
LocationViewController.m
[code]#import "LocationViewController.h" #import <MapKit/MapKit.h> #import "KFAnnotationView.h" #import "KFAnnotion.h" @interface LocationViewController () <MKMapViewDelegate,CLLocationManagerDelegate> @property (weak, nonatomic) IBOutlet MKMapView *mapView; @property (strong, nonatomic) CLLocationManager *locationManager; @end @implementation LocationViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 设置代理 self.mapView.delegate = self; // iOS8请求授权(方式1) if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [self.locationManager requestAlwaysAuthorization]; [self.locationManager requestWhenInUseAuthorization]; } // 设置开始定位 [self.locationManager startUpdatingLocation]; // 设置用户跟随模式 self.mapView.userTrackingMode = MKUserTrackingModeFollow; // 地图类型 self.mapView.mapType = MKMapTypeStandard; } #pragma mark - 地图控件方法 /** * 获取用户的位置,更新用户位置,只要用户改变则调用此方法(包括第一次定位到用户位置) * * @param mapView * @param userLocation 大头针模型 */ - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { // 获取当前用户的经纬度 CLLocationCoordinate2D coordinate = userLocation.location.coordinate; NSLog(@"latitude = %f, longitude = %f",coordinate.latitude,coordinate.longitude); // 设置显示用户的位置,设置地图显示范围(如果不进行区域设置会自动显示区域范围并指定当前用户位置为地图中心点) // iOS8会默认当前位置为中心点 // CLLocationCoordinate2D center = userLocation.location.coordinate; // MKCoordinateSpan span = MKCoordinateSpanMake(0.052996, 0.039880); // MKCoordinateRegion regin = MKCoordinateRegionMake(center, span); // [mapView setRegion:regin animated:YES]; } /** * 滑动地图,区域发生改变时执行 * * @param mapView 地图 * @param animated */ - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated { // 打印滑动地域的位置 NSLog(@"latitude = %f longitude = %f ,latitudeDetal = %f longitude = %f",mapView.region.center.latitude,mapView.region.center.longitude,mapView.region.span.latitudeDelta,mapView.region.span.longitudeDelta); } #pragma mark - 懒加载 - (CLLocationManager *)locationManager { if (!_locationManager) { self.locationManager = [[CLLocationManager alloc]init]; } return _locationManager; } #pragma mark - 显示大头针时调用 /** * 显示大头针时调用,注意方法中的annotation参数是即将显示的大头针对象 * * @param mapView 地图视图 * @param annotation 即将显示的大头针对象 * * @return 显示的大头针对象 */ - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { // 如果是系统的大头针,返回nil if ([annotation isKindOfClass:[MKUserLocation class]]) return nil; // 创建自定义的AnnotationView KFAnnotationView *annotationView = [KFAnnotationView annotationViewWithMapView:mapView]; // 传递数据模型 annotationView.annotation = annotation; return annotationView; } #pragma mark - 添加大头针 - (IBAction)addAnnotion:(UIButton *)sender { KFAnnotion *annotion1 = [[KFAnnotion alloc]init]; annotion1.title = @"幸福时光KTV"; annotion1.subtitle = @"北京市海淀区中关村"; annotion1.coordinate = CLLocationCoordinate2DMake(39.68, 116.16); annotion1.icon = @"category_2"; [self.mapView addAnnotation:annotion1]; KFAnnotion *annotion2 = [[KFAnnotion alloc]init]; annotion2.title = @"如家酒店"; annotion2.subtitle = @"北京市海淀区中关村"; annotion2.coordinate = CLLocationCoordinate2DMake(39.69, 116.19); annotion2.icon = @"category_3"; [self.mapView addAnnotation:annotion2]; } #pragma mark - 下落动画 /** * 自定义下落动画 * * @param mapView * @param views 所有的MKAnnotationView */ - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { for (MKPinAnnotationView *annotationView in views) { // 判断是否是当前的大头针(蓝色定位点) if (![annotationView isKindOfClass:[MKAnnotationView class]]) { CGRect endFrame = annotationView.frame; annotationView.frame = CGRectMake(endFrame.origin.x, 0, endFrame.size.width, endFrame.size.height); [UIView animateWithDuration:0.5 animations:^{ annotationView.frame = endFrame; }]; } } } #pragma mark - 点击回到当前位置 - (IBAction)LocateAction:(UIButton *)sender { // 获得经纬度 CLLocationCoordinate2D coordinate = self.mapView.userLocation.location.coordinate; // 获取区域跨度 MKCoordinateSpan span = MKCoordinateSpanMake(0.052996, 0.039880); // 返回当前区域 MKCoordinateRegion regin = MKCoordinateRegionMake(coordinate, span); [self.mapView setRegion:regin animated:YES]; } @end
实现效果图:
定位在当前位置
点击添加大头针,会显示两颗大头针
导航设置(使用苹果自带的高德地图) 和画出两个位置的路线图
有时候我们需要在地图上进行标注、收藏或者浏览相关路线等,这时我们希望能够打开地图程序进行相应的功能操作。苹果为我们提供了相关的API:MKMapItem。
MKMapItem的特点如下:是OC API , 可以通过一个或者多个pins来打开地图 , 直接转至某个地方 , 定制地图的显示。
MKMapItem的相关属性和方法:
[code]@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; // 根据MKPlacemark地标创建 - (BOOL)openInMapsWithLaunchOptions:(NSDictionary *)launchOptions; // 用于在地图上标注一个位置 + (BOOL)openMapsWithItems:(NSArray *)mapItems launchOptions:(NSDictionary *)launchOptions; // 可以标注多个位置外 , 进行多个位置之间的驾驶导航
launchOptions的可选项:
MKLaunchOptionsDirectionsModeKey 路线模式,常量
MKLaunchOptionsDirectionsModeDriving 驾车模式
MKLaunchOptionsDirectionsModeWalking 步行模式
MKLaunchOptionsMapTypeKey 地图类型,枚举
MKMapTypeStandard :标准模式
MKMapTypeSatellite :卫星模式
MKMapTypeHybrid :混合模式
MKLaunchOptionsShowsTrafficKey 是否显示交通状况,布尔型
MKLaunchOptionsCameraKey 3D地图效果,MKMapCamera类型
注意:此属性从iOS7及以后可用,前面的属性从iOS6开始可用
MKPlacemark:类似于CLPlacemark,只是它在MapKit框架中,可以根据CLPlacemark创建MKPlacemark,它存储了经纬度信息。
[code]- (instancetype)initWithPlacemark:(CLPlacemark *)placemark
MKDirectionsRequest
MKDirectionsRequest 有一些其他控制返回路线信息的属性,如下:
1、departureDate 和arrivalDate。设置这些值,由于旅行时间的限制,将优化返回的路线,例如,会考虑到标准的路况信息。
2、TransportType。目前苹果通过枚举值MKDirectionsTransportTypeAutomobile 或者MKDirectionsTransportTypeWalking提供步行或者驾车方式。默认值是MKDirectionsTransportTypeAny。
3、RequestsAlternateRoutes。如果路由服务器可以找出多条合理的路线,设置YES将会返回所有路线。否则,只返回一条路线。
现在我们已经有了一个可用的请求,可以发送去请求路线。需要使用MKDirections 类,它有一个用MKDirectionsRequest对象构造的函数:
[code]MKDirections *direction = [[MKDirections alloc] initWithRequest:directionsRequest];
MKDirections 类两个方法:
[code]// 计算路线的花费的时间 [direction calculateETAWithCompletionHandler:^(MKETAResponse *response, NSError *error) { }]; // 计算真实的路线 [direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) { }];
两个方法都是异步的,并有一个completion handling块。MKDirections 对象也有一个取消方法,提供给当前任何正在运行的请求。还有calculating属性,如果当前有个请求正在执行,返回为true。单个的MKDirections对象一次只能运行一个请求,额外去请求将会失败。如果你想运行多个并发的请求,你需要创建多个MKDirections对象。但注意,请求太多的话可能会导致苹果服务器因为节流返回错误。
MKDirectionsResponse(方向指南响应)
从苹果服务器回送的对象是MKDirectionsResponse,还有源和目的地,包括MKRoute对象数组。注意,这个数组只有一个对象除非我们设置setrequestsAlternateRoutes为YES。
MKRoute 对象,如它的名字,代表用户选择的两点之间的路线。它包含一些关于路线信息的属性:
[code]@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; // overall route transport type @property (nonatomic, readonly) MKPolyline *polyline; // MKPolyline代表地图上路径。可以画在MKMapView上,下面介绍。 @property (nonatomic, readonly) NSArray *steps; // MKRouteStep 对象的数组,***路线的
渲染polyline
我们已经收到路线的polyline,我们想把它体现到地图上。IOS7之后改变了地图渲染的方法,通过MKOverlayRenderer类。如果我们想做自定义形状或者非标准渲染技术,可以定义一个子类。但是,许多叠加渲染技术都是为标准用例使用的。
我们想渲染一个polyline,可以使用对象MKPolylineRenderer。我们创建MKPolylineRenderer对象,它是MKOverlayRenderer的子类,目的是为了绘制polyline叠加层。我们设置了一些简单的属性(strokeColor 和 lineWidth),以便于可以看到叠加层,然后返回新的对象。
MKOverlayPathRenderer的一些常用属性:
[code]#if TARGET_OS_IPHONE @property (strong) UIColor *fillColor; // 填充颜色 @property (strong) UIColor *strokeColor; // 边框颜色 #else @property (strong) NSColor *fillColor; @property (strong) NSColor *strokeColor; #endif @property CGFloat lineWidth; // 线段的宽 @property CGLineJoin lineJoin; // defaults to kCGLineJoinRound @property CGLineCap lineCap; // defaults to kCGLineCapRound @property CGFloat miterLimit; // defaults to 10 @property CGFloat lineDashPhase; // defaults to 0
当叠加层添加到地图时调用mapView的代理方法:
[code]// 渲染地图覆盖物时触发 - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay
下面是示例代码:
NavigateViewController.m
[code]#import "NavigateViewController.h" #import "KFAnnotion.h" #import <MapKit/MapKit.h> @interface NavigateViewController () <MKMapViewDelegate> @property (weak, nonatomic) IBOutlet UITextField *distinationLable; @property (weak, nonatomic) IBOutlet MKMapView *mapView; @property (strong, nonatomic) CLLocationManager *locationManager; @property (strong, nonatomic) CLGeocoder *geocoder; @end @implementation NavigateViewController - (void)viewDidLoad { [super viewDidLoad]; // iOS请求授权 (方式2) if ([CLLocationManager locationServicesEnabled]) { // 判断当前版本是否为iOS8 if ([[[UIDevice currentDevice] systemVersion]floatValue] >= 8.0) { [self.locationManager requestAlwaysAuthorization]; } } // 设置开始定位 [self.locationManager startUpdatingLocation]; // 设置用户跟随模式 self.mapView.userTrackingMode = MKUserTrackingModeFollow; // 设置代理 self.mapView.delegate = self; } #pragma mark - 懒加载 - (CLLocationManager *)locationManager { if (!_locationManager) { self.locationManager = [[CLLocationManager alloc]init]; } return _locationManager; } - (CLGeocoder *)geocoder { if (!_geocoder) { self.geocoder = [[CLGeocoder alloc]init]; } return _geocoder; } #pragma mark - 当前用户位置 /** * 获取用户的当前位置 * * @param mapView * @param userLocation 用户的位置信息,显示系统大头针 */ - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { NSLog(@"%@",userLocation); } #pragma mark - 导航(进入系统的地图) - (IBAction)navigate:(UIButton *)sender { [self.view endEditing:YES]; if (self.distinationLable.text.length == 0) return; // 进行地理编码,获得将到达的位置 [self.geocoder geocodeAddressString:self.distinationLable.text completionHandler:^(NSArray *placemarks, NSError *error) { if (!error) { // 获取CLPlacemark(地标) CLPlacemark *mark = [placemarks firstObject]; // 根据CLPlacemark对象获得MKPlacemark对象 MKPlacemark *MKmark = [[MKPlacemark alloc]initWithPlacemark:mark]; // 获取起始位置 MKMapItem *sourceItem = [MKMapItem mapItemForCurrentLocation]; // 创建达到的MKMapItem MKMapItem *distinationItem = [[MKMapItem alloc]initWithPlacemark:MKmark]; // 开始导航 [self startNavigateFromSourceItem:sourceItem toDistinationItem:distinationItem]; // NSArray *mapItems = @[sourceItem,distinationItem]; // // NSDictionary *options = @{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey:@YES}; // // // 进入系统的导航界面 // [MKMapItem openMapsWithItems:mapItems launchOptions:options]; } }]; } /** * 导航设置 * * @param sourceItem 起始地 * @param distinationItem 目的地 */ - (void)startNavigateFromSourceItem:(MKMapItem *)sourceItem toDistinationItem:(MKMapItem *)distinationItem { NSArray *mapItems = @[sourceItem,distinationItem]; NSDictionary *options = @{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey:@YES}; // 进入系统的导航界面 [MKMapItem openMapsWithItems:mapItems launchOptions:options]; } #pragma mark - 画出当前位置和目的地路线图 - (IBAction)drawRoute:(UIButton *)sender { [self.view endEditing:YES]; // 获取当前位置 MKMapItem *sourceItem = [MKMapItem mapItemForCurrentLocation]; // 进行地理编码,获得将到达的位置 [self.geocoder geocodeAddressString:self.distinationLable.text completionHandler:^(NSArray *placemarks, NSError *error) { CLPlacemark *CLmark = [placemarks lastObject]; // 通过 CLPlacemark创建 MKPlacemark MKPlacemark *MKmark = [[MKPlacemark alloc]initWithPlacemark:CLmark]; // 添加大头针 KFAnnotion *annotation = [[KFAnnotion alloc]init]; annotation.coordinate = MKmark.location.coordinate; annotation.title = MKmark.locality ? MKmark.administrativeArea : MKmark.locality; annotation.subtitle = MKmark.name; [self.mapView addAnnotation:annotation]; // 创建目的地的位置 MKMapItem *distinationItem = [[MKMapItem alloc]initWithPlacemark:MKmark]; // 画出路线 [self drawRouteFromSourceItem:sourceItem toDistinationItem:distinationItem]; }]; } /** * 画出目的地和当前位置的路线图 * * @param sourceItem 起始地 * @param distinationItem 目的地 */ - (void)drawRouteFromSourceItem:(MKMapItem *)sourceItem toDistinationItem:(MKMapItem *)distinationItem { // 创建请求 MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init]; // 设置起始点和终点的MKMapItem request.source = sourceItem; request.destination = distinationItem; // 创建MKDirections MKDirections *direction = [[MKDirections alloc]initWithRequest:request]; // 使用MKDirections请求数据(可以请求导航的线路) [direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) { // 遍历所有的路线 for (MKRoute *route in response.routes) { NSLog(@"%f---%f",route.distance,route.expectedTravelTime/3600); // 添加遮盖 [self.mapView addOverlay:route.polyline]; } }]; } /** * 添加遮盖调用 * * @param mapView * @param overlay 路线 * * @return 路线 */ - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay { // 创建遮盖的渲染对象 MKPolylineRenderer *ployRenderer = [[MKPolylineRenderer alloc]initWithPolyline:overlay]; // 设置线段的宽 ployRenderer.lineWidth = 5; // 设置线段的边框颜色 ployRenderer.strokeColor = [UIColor redColor]; // 设置填充色 ployRenderer.fillColor = [UIColor purpleColor]; return ployRenderer; } @end
实现结果:
使用系统导航:
两个位置的路线图:
相关文章推荐
- zrclistview
- C++虚函数与纯虚函数的区别
- 合并两个排序的链表
- jquery easyui 实现级联效果
- Unity打包成APK的一些触屏操作
- 模块和包的概念
- Android SVN
- 深度优先搜索和广度优先搜索的代码实现
- PostgreSQL与MySQL比较
- 如何更新linux服务器的系统时间
- Java笔记:CountDownLatch - 计数锁存器、Future、CyclicBarrier - 循环屏障 和 Semaphore - 信号量
- 剑指 offer:矩形覆盖
- 点击tr实现选择checkbox功能,点击checkobx的时候阻止冒泡事件
- Fetching https://dl-ssl.google.com/***************
- DrawText()
- 小马哥--高仿苹果6 主板型号S106刷机 拆机主板图与开机界面图 多图展示
- liunx操作系统实现u盘的挂载和读取----redhut
- 选项卡制作
- Chrome 扩展 最近的历史 HistoryBar v1.1
- mysql 异步执行 query //@todo