拿到终点经纬度就可以导航了
手机上的导航方式,分应用内导航和应用外导航。应用内导航是指使用地图服务提供的SDK(如高德,百度等等),直接将导航功能嵌入到我们自己的APP内部。但是这个方案接入要一定的时间,还会增加APP的内存占用,并且难以实现。应用外导航是指以URL跳转的方式(在iOS中就是以URL Scheme的方式),直接跳到对应的地图APP中,直接利用对方的功能来导航。这样的优点,一是接入方便,二是不增加自己APP的开销。缺点就是如果用户没有装这个地图,应用就没办法使用这个地图的服务。
#import "LGTestViewController.h"
#import <MapKit/MapKit.h>
@interface LGTestViewController ()
@end
@implementation LGTestViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self iphoneMap:@{@"start_address":@"我的位置",
@"end_address":@"终点",
@"start_lat":@"39.1138003159",
@"start_lng":@"117.2165143490",
@"end_lat":@"39.1042806705",
@"end_lng":@"117.2229087353"}];
[self gaodeMap:@{@"start_address":@"我的位置",
@"end_address":@"终点",
@"start_lat":@"39.1138003159",
@"start_lng":@"117.2165143490",
@"end_lat":@"39.1042806705",
@"end_lng":@"117.2229087353"}];
}
//高德地图 文档地址:http://lbs.amap.com/api/amap-mobile/gettingstarted
- (void)gaodeMap:(NSDictionary *)dic {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"iosamap://navi?sourceApplication=etravel&backScheme=etravel&lat=%@&lon=%@&dev=0&style=2",dic[@"end_lat"],dic[@"end_lng"]]];
if ([[UIDevice currentDevice].systemVersion integerValue] >= 10) {
//iOS10以后,使用新API
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
NSLog(@"scheme调用结束");
}];
} else {
//iOS10以前,使用旧API
[[UIApplication sharedApplication] openURL:url];
}
}
//手机地图
- (void)iphoneMap:(NSDictionary *)dic {
//起点
CLLocationCoordinate2D from = CLLocationCoordinate2DMake([dic[@"start_lat"] doubleValue],[dic[@"start_lng"] doubleValue]);
MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:from addressDictionary:nil]];
currentLocation.name = dic[@"start_address"];
//终点
CLLocationCoordinate2D to = CLLocationCoordinate2DMake([dic[@"end_lat"] doubleValue],[dic[@"end_lng"] doubleValue]);
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:to addressDictionary:nil]];
toLocation.name = dic[@"end_address"];
NSArray *items = [NSArray arrayWithObjects:currentLocation, toLocation,nil];
NSDictionary *options = @{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard],
MKLaunchOptionsShowsTrafficKey:@YES
};
//打开苹果自身地图应用
[MKMapItem openMapsWithItems:items launchOptions:options];
}
@end