导航
2016-06-12 本文已影响33人
lee_moons
导入MapKit框架和设置属性
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (weak, nonatomic) IBOutlet UITextField *addressTF;
@end
在文本输入框输入地址并点击搜索按钮,这个会调用系统自带的地图进行搜索
- (IBAction)beginNav:(id)sender
{
// 起始点 终点 -->向苹果请求数据
// 1.地理编码 -->获取终点的CLPlacemark
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:self.addressTF.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (error) {
NSLog(@"%@",error);
return ;
}
// 2.获取终点的CLPlacemark
CLPlacemark *placemark = placemarks[0];
// 3.转换类型
MKPlacemark *mkPlacemark = [[MKPlacemark alloc] initWithPlacemark:placemark];
// 4.获取终点的item
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:mkPlacemark];
// 5.获取起始点的item
MKMapItem *currentMapItem = [MKMapItem mapItemForCurrentLocation];
/*
MKLaunchOptionsDirectionsModeKey
MKLaunchOptionsMapTypeKey
MKLaunchOptionsShowsTrafficKey
*/
NSMutableDictionary *options = [NSMutableDictionary dictionary];
/*
MKLaunchOptionsDirectionsModeDriving 驾车
MKLaunchOptionsDirectionsModeWalking 步行
MKLaunchOptionsDirectionsModeTransit 公共交通
*/
// 导航模式
options[MKLaunchOptionsDirectionsModeKey] = MKLaunchOptionsDirectionsModeDriving;
// 地图类型
options[MKLaunchOptionsMapTypeKey] = MKMapTypeStandard;
// 是否显示交通状况
options[MKLaunchOptionsShowsTrafficKey] = @(YES);
// 6.打开系统自带的地图去导航
[MKMapItem openMapsWithItems:@[currentMapItem,mapItem] launchOptions:options];
}];
}
导航划线
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()<MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (weak, nonatomic) IBOutlet UITextField *addressTF;
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
创建对象请求授权以及设置代理
- (void)viewDidLoad {
[super viewDidLoad];
// 创建对象并请求授权
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager requestWhenInUseAuthorization];
// 设置代理
self.mapView.delegate = self;
}
开始划线
// 1.地理编码 -->获取终点的mapItem
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:self.addressTF.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (error) {
NSLog(@"%@",error);
return ;
}
// 获取地标
CLPlacemark *placemark = placemarks[0];
// 转换类型
MKPlacemark *mkPlacemark = [[MKPlacemark alloc] initWithPlacemark:placemark];
// 2.终点的mapItem
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:mkPlacemark];
// 3.起点的mapItem
MKMapItem *currentMapItem = [MKMapItem mapItemForCurrentLocation];
//direction :方向
// 4.请求数据
// 创建方向请求
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
// 终点
request.destination = mapItem;
// 起点
request.source = currentMapItem;
// 创建方向对象
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
// 请求数据
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"error:%@",error);
return ;
}
// polyline:折线
// 5.获取数据
for (MKRoute *route in response.routes) {
// 获取折线
MKPolyline *polyline = route.polyline;
// 6.添加到地图上
[self.mapView addOverlay:polyline];
}
}];
}];
当有覆盖物添加到地图上时调用
overlay :添加的覆盖物
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
// 创建渲染对象
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
// 设置颜色
renderer.strokeColor = [UIColor redColor];
return renderer;
}