一咽瓷、簡(jiǎn)介
- 路線也是一個(gè)覆蓋層
- 理論指導(dǎo):在地圖上操作覆蓋層,其實(shí)操作的是覆蓋層的數(shù)據(jù)模型
- 添加覆蓋層:在地圖上添加覆蓋層數(shù)據(jù)模型
- 刪除覆蓋層:在地圖上移除覆蓋層數(shù)據(jù)模型
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
MKPolylineRenderer *line;(設(shè)置線寬和顏色)
二、基本使用
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<MKMapViewDelegate>
/** 地理編碼 */
@property (nonatomic, strong) CLGeocoder *geoC;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
@implementation ViewController
//** 路線也是一個(gè)覆蓋層 ** MVC
//** 理論指導(dǎo):在地圖上操作覆蓋層,其實(shí)操作的是覆蓋層的數(shù)據(jù)模型 **
//** 添加覆蓋層:在地圖上添加覆蓋層數(shù)據(jù)模型 **
//** 刪除覆蓋層:在地圖上移除覆蓋層數(shù)據(jù)模型 **
#pragma mark -懶加載
-(CLGeocoder *)geoC
{
if (!_geoC) {
_geoC = [[CLGeocoder alloc] init];
}
return _geoC;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 測(cè)試, 添加圓形覆蓋層
// == 添加圓形覆蓋層 數(shù)據(jù)模型
// 創(chuàng)建一個(gè)圓形的覆蓋層數(shù)據(jù)模型
MKCircle *circle = [MKCircle circleWithCenterCoordinate:self.mapView.centerCoordinate radius:1000000];
[self.mapView addOverlay:circle];
// [self.geoC geocodeAddressString:@"廣州" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// // 廣州地標(biāo)
// CLPlacemark *gzPL = [placemarks firstObject];
//
// [self.geoC geocodeAddressString:@"上海" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// // 上海地標(biāo)
// CLPlacemark *shPL = [placemarks firstObject];
//
// [self getRouteWithBeginPL:gzPL endPL:shPL];
//
// }];
// }];
//
}
- (void)getRouteWithBeginPL:(CLPlacemark *)beginPL endPL:(CLPlacemark *)endPL
{
// 請(qǐng)求導(dǎo)航路線信息
// 創(chuàng)建一個(gè)獲取導(dǎo)航路線信息的請(qǐng)求
MKDirectionsRequest *reqeust = [[MKDirectionsRequest alloc] init];
// 設(shè)置起點(diǎn)和終點(diǎn)
CLPlacemark *sourceCLPL = beginPL;
MKPlacemark *sourcePL = [[MKPlacemark alloc] initWithPlacemark:sourceCLPL];
MKMapItem *sourceItem = [[MKMapItem alloc] initWithPlacemark:sourcePL];
reqeust.source = sourceItem;
// 設(shè)置終點(diǎn)
// 終點(diǎn)
CLPlacemark *destCLPL = endPL;
MKPlacemark *destPL = [[MKPlacemark alloc] initWithPlacemark:destCLPL];
MKMapItem *destItem = [[MKMapItem alloc] initWithPlacemark:destPL];
reqeust.destination = destItem;
// 創(chuàng)建一個(gè)請(qǐng)求導(dǎo)航路線的對(duì)象
MKDirections *directions = [[MKDirections alloc] initWithRequest:reqeust];
// 發(fā)起請(qǐng)求,獲取導(dǎo)航路線
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error)
{
// 獲取路線信息成功
if (error == nil) {
/**
* MKDirectionsResponse : 路線響應(yīng)對(duì)象
* routes : 所有的路線 <MKRoute 路線對(duì)象>
*/
/**
* MKRoute
* name : 路線名稱(chēng)
* advisoryNotices : 警告提示信息
* distance : 距離
* expectedTravelTime : 預(yù)期時(shí)間
* transportType : 通過(guò)方式
* polyline : 幾何路線對(duì)應(yīng)的路線數(shù)據(jù)模型
* steps : 每一步怎么走
*/
/**
* MKRouteStep
* instructions : 行走介紹
* notice : 警告信息
* polyline : 每一節(jié)路線對(duì)應(yīng)的數(shù)據(jù)模型
* distance : 距離
* transportType : 通過(guò)方式
*/
MKRoute *route = [response.routes firstObject];
// 如果想要添加一個(gè)覆蓋層(路線==覆蓋層), 就直接添加覆蓋層對(duì)應(yīng)的數(shù)據(jù)模型
// [self.mapView addOverlay:route.polyline];
// [response.routes enumerateObjectsUsingBlock:^(MKRoute * _Nonnull route, NSUInteger idx, BOOL * _Nonnull stop) {
//
// NSLog(@"路線名稱(chēng):%@---距離--%f", route.name, route.distance);
//
// [route.steps enumerateObjectsUsingBlock:^(MKRouteStep * _Nonnull step, NSUInteger idx, BOOL * _Nonnull stop) {
//
// NSLog(@"%@", step.instructions);
//
//
// }];
//
//
//
// }];
}
}];
}
#pragma mark -MKMapViewDelegate
/**
* 當(dāng)我們添加一個(gè)覆蓋層數(shù)據(jù)模型時(shí), 系統(tǒng)就會(huì)調(diào)用這個(gè)方法查找對(duì)應(yīng)的渲染圖層
*
* @param mapView 地圖
* @param overlay 覆蓋層數(shù)據(jù)模型
*
* @return 渲染層
*/
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
MKCircleRenderer *render = [[MKCircleRenderer alloc] initWithOverlay:overlay];
render.fillColor = [UIColor greenColor];
render.alpha = 0.6;
return render;
}
- (void)ADDLine:(id<MKOverlay>)overlay
{
NSLog(@"獲取渲染圖層");
MKPolylineRenderer *render = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
// 設(shè)置線寬
render.lineWidth = 3;
// 設(shè)置線的顏色
render.strokeColor = [UIColor redColor];
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者