导航

作者: lee_moons | 来源:发表于2016-06-12 16:35 被阅读33次

导入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;
}

相关文章

  • 前段常用单词

    一、导航类 导航:nav 主导航:mainbav 子导航:subnav 顶导航:topnav 边导航:sideba...

  • 移动导航类型

    tab 导航 抽屉导航 list导航 九宫格导航 复合导航

  • APP导航设计模式

    导航主要分为两大类,横向和纵向。横向的导航有:标签导航、舵式导航、tab导航、轮播导航;纵向的导航有:宫格导航、下...

  • 标签式导航的设计思路

    导航分很多类别,如标签导航、抽屉式导航、九宫格导航、混合组合导航、列表式导航、Tab导航、平铺导航等等。之前在我的...

  • Bootstrap-导航菜单-2018.07.07

    Bootstrap-导航菜单 nav 标签式导航 胶囊式导航 垂直胶囊式导航 两端对齐导航 导航元素下拉菜单

  • iOS应用导航模式

    IOS导航模式: 标签导航模式、桌面式模式、抽屉式导航、列表式导航、 点聚式导航、下拉菜单式导航、轮播导航 接下来...

  • Tailwind Navbar

    导航栏 PC导航栏仿Airbnb导航条 PC导航条添加图标 PC导航条上图下字 移动端导航条

  • 程序员导航

    转载自:程序员不可不知的导航 导航网址 极客导航 导航湾 nice导航 GitFox 程序员工具箱 沃克导航 去瞧瞧导航

  • 李亚涛:网址导航网站提交

    大家对网址导航应该不陌生,比如HAO123网址导航,114网址导航,2345导航,360导航,搜狗网址导航等等 网...

  • 交互设计学习_2_移动产品模式设计之导航

    在信息架构设计环节中需要设计产品的页面导航,常见的一级导航有标签导航、分段式导航、抽屉导航、舵式导航;二级导航有列...

网友评论

      本文标题:导航

      本文链接:https://www.haomeiwen.com/subject/xhgedttx.html