利用系统APP实现导航---By张秀清

苹果系统本身自带一个地图APP,但是功能并不是很强大,但是一些简单的导航功能还是能做出来的,下面贴上我的代码

 

//
//  ViewController.m
//  系统APP导航
//
//  Created by 张秀清 on 15/5/4.
//  Copyright (c) 2015年 张秀清. All rights reserved.
//

#import "ViewController.h"

#import <MapKit/MapKit.h>

@interface ViewController ()
/**
 *  输入起点
 */
@property (nonatomic,strong) UITextField       *startTF;
/**
 *  输入终点
 */
@property (nonatomic,strong) UITextField       *endTF;
/**
 *  开始导航按钮
 */
@property (nonatomic,strong) UIButton          *navigateButton;
/**
 *  地理编码器
 */
@property (nonatomic,strong) CLGeocoder        *geocoder;

@end

@implementation ViewController

#pragma mark - 懒加载
-(CLGeocoder *)geocoder{
    
    if (!_geocoder) {
        _geocoder = [[CLGeocoder alloc]init];
    }
    return _geocoder;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
}

#pragma mark - 初始化UI
-(void)initUI{
    
    _startTF = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 150, 50)];
    _startTF.borderStyle = UITextBorderStyleRoundedRect;
    _startTF.placeholder = @"begin";
    [self.view addSubview:_startTF];
    
    _endTF = [[UITextField alloc]initWithFrame:CGRectMake(50, 150, 150, 50)];
    _endTF.borderStyle = UITextBorderStyleRoundedRect;
    _endTF.placeholder = @"end";
    [self.view addSubview:_endTF];
    
    _navigateButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _navigateButton.frame = CGRectMake(50, 200, 100, 50);
    [_navigateButton setTitle:@"start" forState:UIControlStateNormal];
    [_navigateButton addTarget:self action:@selector(startNavigation) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_navigateButton];
}

#pragma mark - 开始导航
-(void)startNavigation{
    
    [self.geocoder geocodeAddressString:self.startTF.text completionHandler:^(NSArray *placemarks, NSError *error) {
       
        /**
         *  获取初始位置信息
         */
        if (placemarks.count == 0) {
            return ;
        }
        CLPlacemark *startCLPlacemark = [placemarks lastObject];
        
        [self.geocoder geocodeAddressString:self.endTF.text completionHandler:^(NSArray *placemarks, NSError *error) {
            /**
             *  获取终点位置信息
             */
            if (placemarks.count == 0) {
                return ;
            }
            CLPlacemark *endCLPlacemark = [placemarks lastObject];
            
            //开始导航
            [self navigateWithStartCLPlacemark:startCLPlacemark endCLPlacemark:endCLPlacemark];
        }];
        
    }];
}

//开始导航
-(void)navigateWithStartCLPlacemark:(CLPlacemark *)startCLPlacemark endCLPlacemark:(CLPlacemark *)endCLPlacemark{
    
    MKPlacemark *startMKPlacemark = [[MKPlacemark alloc]initWithPlacemark:startCLPlacemark];
    MKPlacemark *endMKPlacemark = [[MKPlacemark alloc]initWithPlacemark:endCLPlacemark];
    
    MKMapItem *startItem = [[MKMapItem alloc]initWithPlacemark:startMKPlacemark];
    MKMapItem *endItem = [[MKMapItem alloc]initWithPlacemark:endMKPlacemark];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[MKLaunchOptionsMapTypeKey] = @(MKMapTypeStandard);//地图模式
    dict[MKLaunchOptionsDirectionsModeKey] = MKLaunchOptionsDirectionsModeDriving;//导航模式(走路/开车)
    
    [MKMapItem openMapsWithItems:@[startItem,endItem] launchOptions:dict];//打开系统地图开始导航

}
@end

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。