判断手机应用是否包含相关地图应用,有就显示,没有就不显示相关跳转按钮
- (void)showSheet {
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择地图" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil, nil];
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]]) {
[sheet addButtonWithTitle:@"苹果地图"];
}
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]){
[sheet addButtonWithTitle:@"百度地图"];
}
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
[sheet addButtonWithTitle:@"高德地图"];
}
[sheet showInView:self.view];
}
跳转对应地图应用内进行导航
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:@"苹果地图"]) {
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
CLLocationCoordinate2D coords2 = CLLocationCoordinate2DMake(23.05,113.15);
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords2 addressDictionary:nil]];
//目的地名字
toLocation.name = self.addressString;
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
} else if ([title isEqualToString:@"高德地图"]) {
NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&did=BGVIS2&dname=%@&dev=0&t=0",@"浙医二院"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
} else if ([title isEqualToString:@"百度地图"]) {
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/geocoder?src=%@&address=%@",@"andr.baidu.openAPIdemo",@"浙医二院"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
}