您的位置:首页 > 其它

提供多个地图选择

2016-03-29 11:25 176 查看
#import <MapKit/MapKit.h>
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)//用来获取手机的系统,判断系统是多少
设置
UIActionSheetDelegate 代理

@property(strong ,nonatomic)NSMutableArray *availableMaps;
定义可变数组

定义好 地点和终点 (导航的必要条件)

CLLocationCoordinate2D startCoor;
CLLocationCoordinate2D endCoor;

startCoor =CLLocationCoordinate2DMake([YFMapMessage
sharedInstance].coordinate.latitude,[YFMapMessage
sharedInstance].coordinate.longitude);

if (_dataArray.count==1) {
YFMapItemModel *model =
_dataArray[0];

endCoor =
CLLocationCoordinate2DMake(model.merchantsCoord.latitude, model.merchantsCoord.longitude);
}

self.availableMaps=[[NSMutableArray
alloc] init];

// if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { // ios6以下,调用google map
//
// NSString *urlString = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude];
// // @"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude
// urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// NSURL *aURL = [NSURL URLWithString:urlString];
// [[UIApplication sharedApplication] openURL:aURL];
// } else { // 直接调用ios自己带的apple map
//
// MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
// MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]];
// toLocation.name = @"to name";
//
// [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
// launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
//
// }

UIButton *btn=[UIButton
buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"开始导航"
forState:UIControlStateNormal];
btn.titleLabel.font=FONT(19);
[btn setTitleColor:[UIColor
redColor] forState:UIControlStateNormal];
[btn addTarget:self
action:@selector(pressMore)
forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:CGRectMake(kScreenWidth-100,
kScreenHeight-120,
80, 40)];
[self.view
addSubview:btn];

-(void)pressMore
{

#if 0
//注释掉通过百度SDK
的导航
if (![self checkServicesInited])
return;
_naviType = BN_NaviTypeReal;
[self startNavi];
#else
[self
availableMapsApps];
UIActionSheet *action = [[UIActionSheet
alloc] init];
[action setTitle:@"请选择地图" ];
[action addButtonWithTitle:@"使用高德地图导航"];
for (NSDictionary *dic
in self.availableMaps) {
[action addButtonWithTitle:[NSString
stringWithFormat:@"使用%@导航", dic[@"name"]]];
}
[action addButtonWithTitle:@"取消"];
action.cancelButtonIndex =
self.availableMaps.count +
1;
action.delegate =
self;
[action showInView:self.view];

#endif

}
- (void)availableMapsApps {
[self.availableMaps
removeAllObjects];

// CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate;
// CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01);
NSString *toName =
@"to name";

if ([[UIApplication
sharedApplication] canOpenURL:[NSURL
URLWithString:@"baidumap://map/"]]){
NSString *urlString = [NSString
stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:%@&mode=transit",
startCoor.latitude,
startCoor.longitude,
endCoor.latitude,
endCoor.longitude, toName];

NSDictionary *dic =
@{@"name":
@"百度地图",
@"url": urlString};
[self.availableMaps
addObject:dic];
}
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",
@"云华时代",
endCoor.latitude,
endCoor.longitude];

NSDictionary *dic =
@{@"name":
@"高德地图",
@"url": urlString};
[self.availableMaps
addObject:dic];
}
if ([[UIApplication
sharedApplication] canOpenURL:[NSURL
URLWithString:@"comgooglemaps://"]]) {
NSString *urlString = [NSString
stringWithFormat:@"comgooglemaps://?saddr=&daddr=%f,%f¢er=%f,%f&directionsmode=transit",
endCoor.latitude,
endCoor.longitude,
startCoor.latitude,
startCoor.longitude];

NSDictionary *dic =
@{@"name":
@"Google Maps",
@"url": urlString};
[self.availableMaps
addObject:dic];
}
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex ==
0) {
// CLLocationCoordinate2D startCoor =
// CLLocationCoordinate2D endCoor =
if (SYSTEM_VERSION_LESS_THAN(@"6.0")) {
// ios6以下,调用google map

NSString *urlString = [[NSString
alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude];
// @"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *aURL = [NSURL
URLWithString:urlString];
[[UIApplication
sharedApplication] openURL:aURL];
} else{//
直接调用ios自己带的apple map

MKMapItem *currentLocation = [MKMapItem
mapItemForCurrentLocation];
MKPlacemark *placemark = [[MKPlacemark
alloc] initWithCoordinate:endCoor
addressDictionary:nil];
MKMapItem *toLocation = [[MKMapItem
alloc] initWithPlacemark:placemark];
toLocation.name =
@"to name";

[MKMapItem
openMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey:
MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber
numberWithBool:YES]}];

}
}else
if (buttonIndex < self.availableMaps.count+1)
{
NSDictionary *mapDic =
self.availableMaps[buttonIndex-1];
NSString *urlString = mapDic[@"url"];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL
URLWithString:urlString];
DLog(@"\n%@\n%@\n%@", mapDic[@"name"], mapDic[@"url"],
urlString);
[[UIApplication
sharedApplication] openURL:url];
}
}

刚开始不能跳转 ,因为要在plist里面设置 跳转到app的 标示

在更新iOS9后原来的方法不起做用了。解决办法:
在info.plist添加白名单
<key>LSApplicationQueriesSchemes</key>
<array>
<string>baidumap</string>
<string>iosamap</string>
<string>comgooglemaps<string>
</array>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: