最簡(jiǎn)單快捷的方法使用高德地圖uri格侯,高德地圖uri的具體使用可在高德地圖官方網(wǎng)站看啸罢,使用非常簡(jiǎn)單
簡(jiǎn)單貼下部分代碼
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];
效果圖.png
這種實(shí)現(xiàn)方式柒室,點(diǎn)擊導(dǎo)航的時(shí)候可以設(shè)置直接跳轉(zhuǎn)到高德地圖履磨,但是如果用戶沒有安裝高德地圖的話蛉抓,就直接跳轉(zhuǎn)到App Store ,用戶體驗(yàn)不是很好
所以后來需求改成蹬耘,點(diǎn)擊了立即導(dǎo)航讓選擇使用本地已經(jīng)安裝的地圖
D6943629-6BA3-4D12-A048-C0D848E9A370.png
那么現(xiàn)在問題出現(xiàn)了芝雪,立即導(dǎo)航的按鈕是高德web頁面的方法,iOS 怎么監(jiān)聽事件呢综苔,于是就搜索了各種方法惩系,最后選擇通過UIWebView 的代理方法監(jiān)聽
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
webview 每次加載都會(huì)調(diào)用這個(gè)方法,可以通過request.url 來判斷用戶是否點(diǎn)擊了立即導(dǎo)航按鈕
代碼如下
- (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(@"點(diǎn)擊了導(dǎo)航======================%@",url.absoluteString);
[self showSheetView];
//注意不要直接 return NO;
returnValue = NO;
}
if ([url.absoluteString containsString:@"wap.amap.com"]) {
returnValue = NO;
}
return returnValue;
}
立即導(dǎo)航的點(diǎn)擊事件截取到之后如筛,然后堡牡,在通過[UIApplication sharedApplication] canOpenURL: 方法來判斷本地安裝了哪些地圖
高德地圖:iosamap://
百度地圖:baidumap://
高德,百度 杨刨,蘋果地圖的跳轉(zhuǎn)方法:本文中跳轉(zhuǎn)到地圖顯示效果是這樣的晤柄,需要傳入終點(diǎn)的經(jīng)緯度
91CC416BB73A4547583E3D6FD6DA6863.png
//高德地圖
- (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調(diào)用結(jié)束"); }];
} 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 {
//出發(fā)點(diǎn)的經(jīng)緯度可以不傳,默認(rèn)是當(dāng)前位置
//終點(diǎn)的位置
CLLocationCoordinate2D tolocation = CLLocationCoordinate2DMake(緯度, 經(jīng)度);
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]}];
}