//
// ViewController.m
// UseSysNavgator
//
// Created by hq on 16/5/18.
// Copyright © 2016年 hanqing. All rights reserved.
//
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@property(nonatomic,strong) CLLocationManager *clm;
@property(nonatomic,strong) CLGeocoder *geo;
- (IBAction)startNav;
@end
@implementation ViewController
-(CLLocationManager *)clm{
if (_clm==nil) {
_clm=[[CLLocationManager alloc]init];
_clm.delegate=self;
if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0) {
[_clm requestAlwaysAuthorization];
}
}
return _clm;
}
-(CLGeocoder *)geo{
if (_geo==nil) {
_geo=[[CLGeocoder alloc]init];
}
return _geo;
}
- (IBAction)startNav {
[[self clm] requestLocation];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
CLLocation *loc=locations.firstObject;
NSLog(@"获取用户位置");
[self.geo reverseGeocodeLocation:loc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//用户当前的位置
CLPlacemark *beginMark=placemarks.firstObject;
MKPlacemark *mkBeginMark=[[MKPlacemark alloc]initWithPlacemark:beginMark];
MKMapItem *beginItem=[[MKMapItem alloc]initWithPlacemark:mkBeginMark];
[self.geo geocodeAddressString:@"上海" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *endMark=placemarks.firstObject;
MKPlacemark *mkEndMark=[[MKPlacemark alloc]initWithPlacemark:endMark];
MKMapItem *endItem=[[MKMapItem alloc]initWithPlacemark:mkEndMark];
NSDictionary *dict=@{
MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey:@(MKMapTypeHybrid),
MKLaunchOptionsShowsTrafficKey:@(YES)
};
[MKMapItem openMapsWithItems:@[beginItem,endItem] launchOptions:dict];
}];
}];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
if (error) {
NSLog(@"获取用户当前位置失败");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
本文介绍了如何利用iOS中的MKMapItem接口,调用系统内置的地图应用,实现从当前位置到指定目的地的导航功能。通过创建MKMapItem对象并设置起点和终点,可以无缝集成地图导航到自己的应用程序中,提供便捷的用户体验。
406

被折叠的 条评论
为什么被折叠?



