提供多个地图选择
来源:互联网 发布:网络连接图标是灰色的 编辑:程序博客网 时间:2024/05/22 03:45
#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([YFMapMessagesharedInstance].coordinate.latitude,[YFMapMessagesharedInstance].coordinate.longitude);
if (_dataArray.count==1) {
YFMapItemModel *model =_dataArray[0];
endCoor =CLLocationCoordinate2DMake(model.merchantsCoord.latitude, model.merchantsCoord.longitude);
}
self.availableMaps=[[NSMutableArrayalloc] 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=[UIButtonbuttonWithType:UIButtonTypeCustom];
[btn setTitle:@"开始导航"forState:UIControlStateNormal];
btn.titleLabel.font=FONT(19);
[btn setTitleColor:[UIColorredColor] forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(pressMore)forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:CGRectMake(kScreenWidth-100,kScreenHeight-120,80, 40)];
[self.viewaddSubview:btn];
-(void)pressMore
{
#if 0
//注释掉通过百度SDK 的导航
if (![self checkServicesInited])
return;
_naviType = BN_NaviTypeReal;
[self startNavi];
#else
[selfavailableMapsApps];
UIActionSheet *action = [[UIActionSheetalloc] init];
[action setTitle:@"请选择地图" ];
[action addButtonWithTitle:@"使用高德地图导航"];
for (NSDictionary *dicin self.availableMaps) {
[action addButtonWithTitle:[NSStringstringWithFormat:@"使用%@导航", dic[@"name"]]];
}
[action addButtonWithTitle:@"取消"];
action.cancelButtonIndex =self.availableMaps.count +1;
action.delegate =self;
[action showInView:self.view];
#endif
}
- (void)availableMapsApps {
[self.availableMapsremoveAllObjects];
// CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate;
// CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01);
NSString *toName =@"to name";
if ([[UIApplicationsharedApplication] canOpenURL:[NSURLURLWithString:@"baidumap://map/"]]){
NSString *urlString = [NSStringstringWithFormat:@"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.availableMapsaddObject:dic];
}
if ([[UIApplicationsharedApplication] canOpenURL:[NSURLURLWithString:@"iosamap://"]]) {
NSString *urlString = [NSStringstringWithFormat:@"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.availableMapsaddObject:dic];
}
if ([[UIApplicationsharedApplication] canOpenURL:[NSURLURLWithString:@"comgooglemaps://"]]) {
NSString *urlString = [NSStringstringWithFormat:@"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.availableMapsaddObject: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 = [[NSStringalloc] 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 = [NSURLURLWithString:urlString];
[[UIApplicationsharedApplication] openURL:aURL];
} else{//直接调用ios自己带的apple map
MKMapItem *currentLocation = [MKMapItemmapItemForCurrentLocation];
MKPlacemark *placemark = [[MKPlacemarkalloc] initWithCoordinate:endCooraddressDictionary:nil];
MKMapItem *toLocation = [[MKMapItemalloc] initWithPlacemark:placemark];
toLocation.name =@"to name";
[MKMapItemopenMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumbernumberWithBool:YES]}];
}
}elseif (buttonIndex < self.availableMaps.count+1) {
NSDictionary *mapDic =self.availableMaps[buttonIndex-1];
NSString *urlString = mapDic[@"url"];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURLURLWithString:urlString];
DLog(@"\n%@\n%@\n%@", mapDic[@"name"], mapDic[@"url"], urlString);
[[UIApplicationsharedApplication] openURL:url];
}
}
刚开始不能跳转 ,因为要在plist里面设置 跳转到app的 标示
在更新iOS9后原来的方法不起做用了。解决办法:
在info.plist添加白名单
<key>LSApplicationQueriesSchemes</key>
<array>
<string>baidumap</string>
<string>iosamap</string>
<string>comgooglemaps<string>
</array>
- 提供多个地图选择
- ASP.NET - 如何:配置多个站点地图和站点地图提供程序
- baidu 地图多个终点选择一个最近的,画出行车线路
- 万能地图下载器中在任务列表中如何快速选择多个任务?
- google 提供的地图
- 使用多个站点地图
- 提供选择删除图片
- 多个Activity-利用BaseActivity提供共性
- 百度地图4.0多个经纬度展示在地图上
- checkbo选择多个删除
- Jquery 选择器,选择多个
- QFileDialog选择多个文件夹
- CFileDialog选择多个文件
- CFileDialog选择多个文件
- jquery选择多个元素
- jquery checkbox 选择多个
- QFileDialog 选择多个文件
- FileDialog选择多个文件
- SQlite数据库的C编程接口(四) 绑定参数(Bound Parameters) ——《Using SQlite》读书笔记
- Java 线程池的原理与实现
- 【BZOJ2435】[Noi2011]道路修建【TreeDP】
- Ubuntu上的ftp配置连接
- Mysql导入官方示例employees问题解决记录
- 提供多个地图选择
- ios 延时加载
- Warshell's Algorithms
- CreateFileMapping
- Struts2的模板和主题theme及自定义theme
- PHP程序员最常犯的11个MySQL错误
- C#多线程编程时遇到跨线程访问异常的处理方法
- 关于包含0长数组的结构体对齐问题(C语言 Linux_x64 GNU编译器)
- 关于汇编语言寄存器和指令操作的整理