美文网首页iOS学习iOS
iOS 利用系统App导航

iOS 利用系统App导航

作者: iOS_成才录 | 来源:发表于2015-11-12 19:33 被阅读363次

    导航的常用三种实现方案

    • 1.可以将需要导航的位置丢给系统的地图APP进行导航
    • 2.发送网络请求到公司服务器获取导航数据, 然后自己手动绘制导航
    • 3.利用三方SDK实现导航(百度)

    利用系统App导航

    • [MKMapItem openMapsWithItems:items launchOptions:md];
    #import "ViewController.h"
    #import <MapKit/MapKit.h>
    #import <CoreLocation/CoreLocation.h>
    
    @interface ViewController ()
    
    /** 地理编码 */
    @property (nonatomic, strong) CLGeocoder *geoC;
    
    @end
    
    @implementation ViewController
    
    #pragma mark -懒加载
    -(CLGeocoder *)geoC
    {
        if (!_geoC) {
            _geoC = [[CLGeocoder alloc] init];
        }
        return _geoC;
    }
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
       [self.geoC geocodeAddressString:@"广州" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
           // 广州地标
           CLPlacemark *gzPL = [placemarks firstObject];
           [self.geoC geocodeAddressString:@"上海" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
               // 上海地标
               CLPlacemark *shPL = [placemarks firstObject];
               
               [self systemNavWithBeginPL:gzPL endPL:shPL];
           }];
       }];
    }
    
    
    /**
     *  根据起点和终点地标对象,调用系统APP进行导航
     *
     *  @param beginCLPL 起点地标
     *  @param endCLPL   终点地标
     */
    -(void)systemNavWithBeginPL:(CLPlacemark *)beginCLPL endPL : (CLPlacemark *)endCLPL
    {
        // 调用系统的APP进行导航
    
        // 地图起点地标对象
        MKPlacemark *beginPL = [[MKPlacemark alloc] initWithPlacemark:beginCLPL];
        // 起点
        MKMapItem *beginItem = [[MKMapItem alloc] initWithPlacemark:beginPL];
        
        // 地图终点地标对象
        MKPlacemark *endPL = [[MKPlacemark alloc] initWithPlacemark:endCLPL];
        // 终点
        MKMapItem *endItem = [[MKMapItem alloc] initWithPlacemark:endPL];
        
        // 起点和终点数组
        NSArray *items = @[beginItem, endItem];
        
        // 设置地图启动项(导航模式:驾驶, 地图类型: 混合, 是否显示交通: 是)
        NSDictionary *dic = @{
                              MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
                              MKLaunchOptionsMapTypeKey : @(MKMapTypeHybrid),
                              MKLaunchOptionsShowsTrafficKey : @(YES)
                              
                              };
        
        // 给定两个点,起点和终点, 然后设置启动项, 开始调用系统APP进行导航
        [MKMapItem openMapsWithItems:items launchOptions:dic];
    }
    @end
    

    相关文章

      网友评论

        本文标题:iOS 利用系统App导航

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