最近项目中遇到一个小需求,就是点击地图上的大头针进行导航,如果手机中装了多种地图软件,就用提示框显示出来,如果没有装地图软件,就默认跳转到苹果自带的地图进行导航,代码如下:
//导航按钮
- (void)guideBtnAction:(UIButton *)button
{
//创建时不指定按钮
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
NSArray *mapArray = @[@"http://maps.apple.com/",@"baidumap://map/",@"iosamap://"];
NSArray *mapNameArray = @[@"苹果地图",@"百度地图",@"高德地图"];
for (int i = 0; i < 3; i ++) {
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:mapArray[i]]]) {
[sheet addButtonWithTitle:mapNameArray[i]];
}
}
//添加取消按钮
[sheet addButtonWithTitle:@"取消"];
sheet.delegate = self;
sheet.cancelButtonIndex = sheet.numberOfButtons - 1;
[sheet showInView:self.view.window];
}
接下来是响应提示框的按钮事件:
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == actionSheet.cancelButtonIndex) {
[actionSheet dismissWithClickedButtonIndex:actionSheet.numberOfButtons - 1 animated:YES];
return;
}
if (actionSheet.numberOfButtons == 4) {
switch (buttonIndex) {
case 0:
[self openNativeAppleMapWithTag];
break;
case 1:
[self openNativeNaviBaiDu];
break;
case 2:
[self openNativiGaoDe];
break;
default:
break;
}
}else if (actionSheet.numberOfButtons == 3){
switch (buttonIndex) {
case 0:
[self openNativeAppleMapWithTag];
break;
case 1:{
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
[self openNativiGaoDe];
}
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]) {
[self openNativeNaviBaiDu];
}
}
break;
default:
break;
}
}else{
switch (buttonIndex) {
case 0:
[self openNativeAppleMapWithTag];
break;
default:
break;
}
}
}
//调用高德地图客户端
- (void)openNativiGaoDe
{
//起点
CLLocation *location = [[CLLocation alloc]initWithLatitude:[_latitude floatValue] longitude:[_longitude floatValue]];
CLLocationCoordinate2D coor = location.coordinate;
//终点
CLLocation *location2 = [[CLLocation alloc]initWithLatitude:[self.pointModel.lat floatValue] longitude:[self.pointModel.lng floatValue]];
CLLocationCoordinate2D coor2 = location2.coordinate;
NSString *url = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=%lf&slon=%lf&sname=我的位置&did=BGVIS2&dlat=%lf&dlon=%lf&dname=%@&dev=0&m=0",coor.latitude,coor.longitude, coor2.latitude - 0.00328,coor2.longitude - 0.01185,self.pointModel.stationname] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
//调用百度地图客户端导航
- (void)openNativeNaviBaiDu
{
NSString *baiduParameterFormat = @"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:终点&mode=driving";
NSString *urlString = [[NSString stringWithFormat:baiduParameterFormat,[_latitude floatValue],[_longitude floatValue],[self.pointModel.lat floatValue],[self.pointModel.lng floatValue]]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
}
//调用苹果地图
- (void)openNativeAppleMapWithTag
{
//起点
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];;
CLLocationCoordinate2D desCorrdinate = CLLocationCoordinate2DMake([self.pointModel.lat floatValue] - 0.00328, [self.pointModel.lng floatValue] - 0.01185);
//终点
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:desCorrdinate addressDictionary:nil]];
toLocation.name = [NSString stringWithFormat:@"%@",self.pointModel.stationname];
//默认驾车
[MKMapItem openMapsWithItems:@[currentLocation,toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
}