{
float startLat=31.232340;
float startLng=121.400200;
float endLat=30.944729;
float endLng=120.900955;
NSString *address=@"测试地址";
if (buttonIndex==1) {
//百度
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]){
NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:%@&mode=driving",//transit
startLat, startLng,endLat,endLng,address];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
}else{
[MBProgressHUD showMessage:@"请安装百度地图"];
}
}
else if (buttonIndex==2){
//谷歌
// if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
// NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?saddr=&daddr=%f,%f¢er=%f,%f&directionsmode=transit", endLat, endLng, startLat, startLng];
// urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// NSURL *url = [NSURL URLWithString:urlString];
// [[UIApplication sharedApplication] openURL:url];
// }
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
NSString *urlString = [NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=applicationScheme&poiname=fangheng&poiid=BGVIS&lat=%f&lon=%f&dev=0&style=3",address,endLat,endLng];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
}
else{
[MBProgressHUD showMessage:@"请安装高德地图"];
}
}
else if (buttonIndex==0){
// CLLocationCoordinate2D startCoor =CLLocationCoordinate2DMake(startLat+0.01, startLng+0.01);
// CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(endLat+0.01, startLng+0.01);
// //苹果自带
// MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
// MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]];
// toLocation.name = address;
// [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
// launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
NSURL * apple_App = [NSURL URLWithString:@"http://maps.apple.com/"];
if ([[UIApplication sharedApplication] canOpenURL:apple_App]) {
NSString *urlString=[NSString stringWithFormat:@"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",startLat,startLng,endLat,endLng];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
}else{
[MBProgressHUD showMessage:@"请安装苹果地图"];
}
}
}
image.png
1】使用canOpenURL方法来检测该手机是否安装相应APP
该方法会返回一个BOOL值,当为YES时,表明已安装该APP
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]
1、苹果自带地图(不需要检测,所以不需要URL Scheme)
2、百度地图 baidumap://
3、高德地图 iosamap://
网友评论