码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-05-04 17:24:54      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

苹果系统本身自带一个地图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

 

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

标签:

原文地址:http://www.cnblogs.com/sixindev/p/4476502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!