iOS应用根据经纬度打开不同地图app进行导航

版权声明:来来来,每人一个“坑”,别急,都有! https://blog.csdn.net/qq_22026331/article/details/55048873

现在地图导航已经很普遍了,为自己的项目添加导航功能有两种方法,一种是集成第三方sdk到自己的app上,这个比较复杂,并且会使自己的app变得很大很臃肿。所以,还是选择另一种方式,根据所获取的经纬度,打开第三方地图app进行导航(百度地图、谷歌地图、高德地图、苹果地图和腾讯地图等等),这种方式的最简单粗暴的。以下是效果图,自己可以体会以下。



这个功能我已经封装在工具类里面了,现在抽取出来参考一下吧!

#pragma mark 地图--------------------------------------
#pragma mark - 选择打开地图导航
- (void)chooseMapWithLocationCoordinate2D:(CLLocationCoordinate2D)coordinate currentController:(ViewController *)viewController appName:(NSString *)appName backScheme:(NSString *)backScheme
{
    // 查看本机所安装的地图app
    _mapArr = [self getInstalledMapAppWithEndLocation:coordinate appName:appName backScheme:backScheme];
    // 创建UIActionSheet
    _mapActionSheet = [[UIActionSheet alloc] initWithTitle:@"请选择地图进行导航" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil];
    for (NSDictionary *dict in _mapArr)
    {
        [_mapActionSheet addButtonWithTitle:dict[@"title"]];
    }
    [_mapActionSheet showInView:viewController.view];
}

#pragma mark - 导航方法
- (NSArray *)getInstalledMapAppWithEndLocation:(CLLocationCoordinate2D)endLocation appName:(NSString *)appName backScheme:(NSString *)backScheme
{
    NSMutableArray *maps = [NSMutableArray array];
    
    //苹果地图
    NSMutableDictionary *iosMapDic = [NSMutableDictionary dictionary];
    iosMapDic[@"title"] = @"苹果地图";
    iosMapDic[@"lat"] = [NSNumber numberWithDouble:endLocation.latitude];
    iosMapDic[@"lng"] = [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=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",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://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",appName,backScheme,endLocation.latitude,endLocation.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,endLocation.latitude, endLocation.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",endLocation.latitude, endLocation.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];
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if ([actionSheet isEqual:_mapActionSheet])
    {
        if (buttonIndex)
        {
            NSDictionary *dic = _mapArr[buttonIndex - 1];
            switch (buttonIndex)
            {
                case 1:
                    [self navAppleMapWithDict:dic];
                    break;
                    
                case 2:
                default:
                {
                    NSString *urlString = dic[@"url"];
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
                }
                    break;
            }
        }
    }
}


百度地图用的坐标系和其他地图是不一样的,所以还要考虑坐标系之间的转换,上面的代码可能还存在坐标系转换的问题,只作为参考!

展开阅读全文

没有更多推荐了,返回首页