1,设置开启开启定位权限
Privacy - Location When In Use Usage Description
2,plist中添加白名单(LSApplicationQueriesSchemes)才能检查某个地图是否已经安装:
高德地图 iosamap
百度地图 baidumap
腾讯地图 qqmap
谷歌地图 comgooglemaps
#pragma mark "选择地图类型进行导航"
static NSArray * _mapArr;
- (void)chooseMapWithLocationCoordinate2D:(CLLocationCoordinate2D)coordinate appName:(NSString *)appName backScheme:(NSString *)backScheme adress:(NSString *)address
{
// 查看本机所安装的地图app
_mapArr = [self getInstalledMapAppWithEndLocation:coordinate appName:appName backScheme:backScheme adress:address];
// 创建UIActionSheet
UIActionSheet * _mapActionSheet = [[UIActionSheet alloc] initWithTitle:@"请选择地图进行导航" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil];
for (NSDictionary *dict in _mapArr)
{
[_mapActionSheet addButtonWithTitle:dict[@"title"]];
}
[_mapActionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex)
{
buttonIndex==1?[self navAppleMapWithDict:_mapArr[buttonIndex-1]]:
[_mapArr[buttonIndex-1][@"url"] openUrl];
}
}
#pragma mark - 导航方法
- (NSArray *)getInstalledMapAppWithEndLocation:(CLLocationCoordinate2D)endLocation appName:(NSString *)appName backScheme:(NSString *)backScheme adress:(NSString *)address
{
NSMutableArray *maps = [NSMutableArray array];
CLLocationCoordinate2D amapcoord = [self bdToGaodeWithLat:endLocation.latitude andLon:endLocation.longitude];
//苹果地图
NSMutableDictionary *iosMapDic = [NSMutableDictionary dictionary];
iosMapDic[@"title"] = @"苹果地图";
iosMapDic[@"lat"] = [NSNumber numberWithDouble:endLocation.latitude];
iosMapDic[@"lon"] = [NSNumber numberWithDouble:endLocation.longitude];
[maps addObject:iosMapDic];
//百度地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
NSMutableDictionary *baiduMapDic = [NSMutableDictionary dictionary];
baiduMapDic[@"title"] = @"百度地图";
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=name:商家位置|latlng:%f,%f&mode=driving",endLocation.latitude,endLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
baiduMapDic[@"url"] = urlString;
[maps addObject:baiduMapDic];
}
//高德地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];
gaodeMapDic[@"title"] = @"高德地图";
NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&backScheme=%@&sname=我的位置&dlat=%lf&dlon=%lf&dname=%@&dev=0&style=2&m=0&t=0",appName,backScheme,amapcoord.latitude,amapcoord.longitude,@"商家位置"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
gaodeMapDic[@"url"] = urlString;
[maps addObject:gaodeMapDic];
}
//谷歌地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
NSMutableDictionary *googleMapDic = [NSMutableDictionary dictionary];
googleMapDic[@"title"] = @"谷歌地图";
NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",appName,backScheme,amapcoord.latitude, amapcoord.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
googleMapDic[@"url"] = urlString;
[maps addObject:googleMapDic];
}
//腾讯地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];
qqMapDic[@"title"] = @"腾讯地图";
NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%f,%f&to=商家位置&coord_type=1&policy=0",amapcoord.latitude, amapcoord.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
qqMapDic[@"url"] = urlString;
[maps addObject:qqMapDic];
}
return maps;
}
// 苹果地图
- (void)navAppleMapWithDict:(NSDictionary *)dict
{
MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake([dict[@"lat"] doubleValue], [dict[@"lng"] doubleValue]) addressDictionary:nil]];
NSArray *items = @[currentLoc,toLocation];
NSDictionary *dic = @{
MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
MKLaunchOptionsShowsTrafficKey : @(YES)
};
[MKMapItem openMapsWithItems:items launchOptions:dic];
}
// 百度转其他地图经纬度
-(CLLocationCoordinate2D)bdToGaodeWithLat:(double)lat andLon:(double)lon
{
// double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
double x = (lon-0.0065);
double y = (lat-0.006);
double z = sqrt(x*x + y*y) - 0.00002*sin(y*M_PI);
double theta = atan2(y, x) - 0.000003 * cos(x * M_PI);
NSLog(@"bd_lon:%f",z * cos(theta) );
NSLog(@"bd_lat:%f",z * sin(theta) );
return CLLocationCoordinate2DMake(z * sin(theta) , z * cos(theta));
}
使用的时候直接调用代码方法:
[self chooseMapWithLocationCoordinate2D:CLLocationCoordinate2DMake([lat floatValue], [lon floatValue]) appName:isApp_Name backScheme:backScheme adress:@"目的地"];