導(dǎo)入MapKit框架和設(shè)置屬性
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (weak, nonatomic) IBOutlet UITextField *addressTF;
@end
在文本輸入框輸入地址并點(diǎn)擊搜索按鈕,這個(gè)會(huì)調(diào)用系統(tǒng)自帶的地圖進(jìn)行搜索
- (IBAction)beginNav:(id)sender
{
// 起始點(diǎn) 終點(diǎn) -->向蘋果請(qǐng)求數(shù)據(jù)
// 1.地理編碼 -->獲取終點(diǎn)的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.獲取終點(diǎn)的CLPlacemark
CLPlacemark *placemark = placemarks[0];
// 3.轉(zhuǎn)換類型
MKPlacemark *mkPlacemark = [[MKPlacemark alloc] initWithPlacemark:placemark];
// 4.獲取終點(diǎn)的item
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:mkPlacemark];
// 5.獲取起始點(diǎn)的item
MKMapItem *currentMapItem = [MKMapItem mapItemForCurrentLocation];
/*
MKLaunchOptionsDirectionsModeKey
MKLaunchOptionsMapTypeKey
MKLaunchOptionsShowsTrafficKey
*/
NSMutableDictionary *options = [NSMutableDictionary dictionary];
/*
MKLaunchOptionsDirectionsModeDriving 駕車
MKLaunchOptionsDirectionsModeWalking 步行
MKLaunchOptionsDirectionsModeTransit 公共交通
*/
// 導(dǎo)航模式
options[MKLaunchOptionsDirectionsModeKey] = MKLaunchOptionsDirectionsModeDriving;
// 地圖類型
options[MKLaunchOptionsMapTypeKey] = MKMapTypeStandard;
// 是否顯示交通狀況
options[MKLaunchOptionsShowsTrafficKey] = @(YES);
// 6.打開系統(tǒng)自帶的地圖去導(dǎo)航
[MKMapItem openMapsWithItems:@[currentMapItem,mapItem] launchOptions:options];
}];
}
導(dǎo)航劃線
#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
創(chuàng)建對(duì)象請(qǐng)求授權(quán)以及設(shè)置代理
- (void)viewDidLoad {
[super viewDidLoad];
// 創(chuàng)建對(duì)象并請(qǐng)求授權(quán)
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager requestWhenInUseAuthorization];
// 設(shè)置代理
self.mapView.delegate = self;
}
開始劃線
// 1.地理編碼 -->獲取終點(diǎn)的mapItem
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:self.addressTF.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (error) {
NSLog(@"%@",error);
return ;
}
// 獲取地標(biāo)
CLPlacemark *placemark = placemarks[0];
// 轉(zhuǎn)換類型
MKPlacemark *mkPlacemark = [[MKPlacemark alloc] initWithPlacemark:placemark];
// 2.終點(diǎn)的mapItem
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:mkPlacemark];
// 3.起點(diǎn)的mapItem
MKMapItem *currentMapItem = [MKMapItem mapItemForCurrentLocation];
//direction :方向
// 4.請(qǐng)求數(shù)據(jù)
// 創(chuàng)建方向請(qǐng)求
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
// 終點(diǎn)
request.destination = mapItem;
// 起點(diǎn)
request.source = currentMapItem;
// 創(chuàng)建方向?qū)ο? MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
// 請(qǐng)求數(shù)據(jù)
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"error:%@",error);
return ;
}
// polyline:折線
// 5.獲取數(shù)據(jù)
for (MKRoute *route in response.routes) {
// 獲取折線
MKPolyline *polyline = route.polyline;
// 6.添加到地圖上
[self.mapView addOverlay:polyline];
}
}];
}];
當(dāng)有覆蓋物添加到地圖上時(shí)調(diào)用
overlay :添加的覆蓋物
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
// 創(chuàng)建渲染對(duì)象
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
// 設(shè)置顏色
renderer.strokeColor = [UIColor redColor];
return renderer;
}