最简单快捷的方法使用高德地图uri,高德地图uri的具体使用可在高德地图官方网站看,使用非常简单
简单贴下部分代码
NSString *urlStr = [NSString stringWithFormat:@"http://uri.amap.com/navigation?from=%f,%f,我的位置&to=%@,%@,%@&mode=car&policy=1&src=mypage&coordinate=gaode&callnative=0",self.currentLocation.coordinate.longitude,self.currentLocation.coordinate.latitude,model.lng,model.lat,model.swjg_mc];
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64)];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString encodeToPercentEscapeString: urlStr]]];
[self.webView loadRequest:request];
self.webView.delegate = self;
[self.view addSubview:self.webView];
这种实现方式,点击导航的时候可以设置直接跳转到高德地图,但是如果用户没有安装高德地图的话,就直接跳转到App Store ,用户体验不是很好
所以后来需求改成,点击了立即导航让选择使用本地已经安装的地图
那么现在问题出现了,立即导航的按钮是高德web页面的方法,iOS 怎么监听事件呢,于是就搜索了各种方法,最后选择通过UIWebView 的代理方法监听
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
webview 每次加载都会调用这个方法,可以通过request.url 来判断用户是否点击了立即导航按钮
代码如下
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
BOOL returnValue = YES;
if ([url.absoluteString containsString:@"iosamap"] || [url.absoluteString containsString:@"amapuri://route/plan/"]) {
NSLog(@"点击了导航======================%@",url.absoluteString);
[self showSheetView];
//注意不要直接 return NO;
returnValue = NO;
}
if ([url.absoluteString containsString:@"wap.amap.com"]) {
returnValue = NO;
}
return returnValue;
}
立即导航的点击事件截取到之后,然后,在通过[UIApplication sharedApplication] canOpenURL: 方法来判断本地安装了哪些地图
高德地图:iosamap://
百度地图:baidumap://
高德,百度 ,苹果地图的跳转方法:本文中跳转到地图显示效果是这样的,需要传入终点的经纬度
//高德地图
- (void)openGaoDeMap{
NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=%f&slon=%f&sname=我的位置&did=BGVIS2&dlat=%@&dlon=%@&dname=%@&dev=0&t=0",self.currentLocationLatitude,self.currentLocationLongitude,self.lat,self.lng,self.addressName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *myLocationScheme = [NSURL URLWithString:urlString];
if ([[UIDevice currentDevice].systemVersion integerValue] >= 10) { //iOS10以后,使用新API
[[UIApplication sharedApplication] openURL:myLocationScheme options:@{} completionHandler:^(BOOL success) { NSLog(@"scheme调用结束"); }];
} else { //iOS10以前,使用旧API
[[UIApplication sharedApplication] openURL:myLocationScheme];
}
}
//百度地图
- (void)openBaiduMap {
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:BAIDUMAP]]) {
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%@,%@|name:%@&mode=driving&coord_type=bd09&origin_region=%@&destination_region=%@",self.lat, self.lng,self.addressName,@"",@""]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
} else {
[ToolKit showAlertMessage:@"" currentVc:self];
}
}
}
//苹果地图
- (void)openAppleMap {
//出发点的经纬度可以不传,默认是当前位置
//终点的位置
CLLocationCoordinate2D tolocation = CLLocationCoordinate2DMake(纬度, 经度);
MKMapItem *currentLocationItem = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocationItem = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:tolocation addressDictionary:@{}]];
toLocationItem.name = self.addressName;
[MKMapItem openMapsWithItems:@[currentLocationItem, toLocationItem]
launchOptions:@{MKLaunchOptionsDirectionsModeKey:
MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}