; radius:100000];
// 2.3 添加覆盖层数据模型到地图上
[self.mapView addOverlay:circle2];
// 3. 调用开始导航的方法(从广州到上海)
[self directionsWithBeginPlackmark:gzP andEndPlacemark:shP];
}];
}];
}
#pragma mark - 开始导航
// ① 根据两个地标,发送网络请求给苹果服务器获取导航数据,请求对应的行走路线信息
- (void)directionsWithBeginPlackmark:(CLPlacemark *)startPLCL andEndPlacemark:(CLPlacemark *)endPLCL{
// 创建请求导航路线数据信息
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
// 创建起点:根据 CLPlacemark 地标对象创建 MKPlacemark 地标对象
MKPlacemark *startplMK = [[MKPlacemark alloc] initWithPlacemark:startPLCL];
request.source = [[MKMapItem alloc] initWithPlacemark:startplMK];
// 创建终点:根据 CLPlacemark 地标对象创建 MKPlacemark 地标对象
MKPlacemark *endplMK = [[MKPlacemark alloc] initWithPlacemark:endPLCL];
request.destination = [[MKMapItem alloc] initWithPlacemark:endplMK];
// 1. 创建导航对象,根据请求,获取实际路线信息
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
// 2. 调用方法, 开始发送请求,计算路线信息
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
// ② 解析导航数据
// block遍历所有的路线方案routes (MKRoute对象)
[response.routes enumerateObjectsUsingBlock:^(MKRoute * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@",obj.advisoryNotices);
NSLog(@"%@,%f,%f",obj.name,obj.distance,obj.expectedTravelTime);
// 添加覆盖层数据模型,路线对应的几何线路模型(由很多点组成)
// 当我们添加一个覆盖层数据模型时, 系统绘自动查找对应的代理方法, 找到对应的覆盖层"视图"
[self.mapView addOverlay:obj.polyline]; // 添加折线
// block 遍历每一种路线的每一个步骤(MKRouteStep对象)
[obj.steps enumerateObjectsUsingBlock:^(MKRouteStep * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@",obj.instructions);// 打印步骤说明
}];
}];
// 拓展
#pragma mark - block遍历
[response.routes enumerateObjectsUsingBlock:^(MKRoute * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"1打印对象的属性:%@,2索引:%lu",obj.name,(unsigned long)idx);
& |