手机地图APP -> 随意跳转

奋斗的七月

在项目中用到了判断用户安装了哪种地图,直接根据经纬度跳转到地图软件中,并且绘制路线,非常实用

------ 判断用户安装了哪种地图 ------
/**
 
 * 判断用户安装了哪种地图
 * @author sweet
 
 * @param parameter
 
 * @return result
 
 */

- (void)checkMap

{
    
    NSMutableArray *mapArray = [NSMutableArray new];
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]){
        
        NSLog(@"外部安装了谷歌地图");
        
        [mapArray addObject:@"Google地图"];
        
    }
    
    if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        
        NSLog(@"外部安装了高德地图");
        
        [mapArray addObject:@"高德地图"];
        
    }
    
    if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"baidumap://"]]){
        
        NSLog(@"外部安装了百度地图");
        
        [mapArray addObject:@"百度地图"];
        
    }
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]){
        
        NSLog(@"外部安装了腾讯地图");
        
        [mapArray addObject:@"腾讯地图"];
        
    }
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"sgmap://"]]){
        
        NSLog(@"外部安装了搜狗地图");
        
        [mapArray addObject:@"搜狗地图"];
        
    }
    
    UIActionSheet *mapSheet = [[UIActionSheet alloc] initWithTitle:@"选择地图" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"苹果地图" otherButtonTitles:nil, nil];
    
    for (int i = 0; i < mapArray.count; i++) {
        
        [mapSheet addButtonWithTitle:mapArray[I]];
        
    }
    
    NSLog(@"mapArray===%@",mapArray);
    
    [mapSheet addButtonWithTitle:@"取消"];
    
    mapSheet.cancelButtonIndex = mapSheet.numberOfButtons - 1;
    
    [mapSheet showInView:self.view];
    
}

------ 跳转到对应的地图软件 ------
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{
    
    NSString *str = [actionSheet buttonTitleAtIndex:buttonIndex];
    
    //---起始点坐标
    NSString *startLatitude = @"34.1800850000";
    NSString *startLongitude = @"108.5706980000";
    
    //---终点坐标
    NSString *endLatitude = @"34.1900850000";
    NSString *endLongitude = @"108.8706980000";
    CLLocationCoordinate2D destinationCoordinate = CLLocationCoordinate2DMake([endLatitude floatValue],[endLongitude floatValue]);
    
    NSString *shopName = @"噢噢噢噢";
    
    NSLog(@"str =%@",str);
    
    if ([str isEqualToString:@"苹果地图"]) {
        
        NSLog(@"自带地图导航");
        
        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
        
        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:destinationCoordinate addressDictionary:nil]];
        
        toLocation.name = @"哈哈";
        
        [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
         
                       launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
        
    }
    
    if ([str isEqualToString:@"高德地图"]) {
        
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
            
            NSString *urlString=[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&backScheme=applicationScheme&slat=%f&slon=%f&sname=我&dlat=%f&dlon=%f&dname=%@&dev=0&m=0&t=1",[startLatitude floatValue],[startLongitude floatValue],destinationCoordinate.latitude,destinationCoordinate.longitude,shopName];
            
            urlString =  [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            
            NSURL *aURL = [NSURL URLWithString:urlString];
            
            [[UIApplication sharedApplication] openURL:aURL];
            
        }
        
    }
    
    if ([str isEqualToString:@"百度地图"]){
        
        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",
                                   
                                   [startLatitude floatValue],[startLongitude floatValue],destinationCoordinate.latitude,destinationCoordinate.longitude,shopName];
            
            urlString =  [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            
            NSURL *aURL = [NSURL URLWithString:urlString];
            
            [[UIApplication sharedApplication] openURL:aURL];
            
        }
        
    }
    
    if ([str isEqualToString:@"Google地图"]) {
        
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
            
            NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?saddr=&daddr=%f,%f¢er=%f,%f&directionsmode=transit",[startLatitude floatValue],[startLongitude floatValue],destinationCoordinate.latitude,destinationCoordinate.longitude];
            
            urlString =  [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            
            NSURL *aURL = [NSURL URLWithString:urlString];
            
            [[UIApplication sharedApplication] openURL:aURL];
            
        }
        
    }
    
    if ([str isEqualToString:@"腾讯地图"]) {
        
        NSString *urlStr = [NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&fromcoord=%f,%f&tocoord=%f,%f&policy=1",[startLatitude floatValue],[startLongitude floatValue],destinationCoordinate.latitude,destinationCoordinate.longitude];
        
        NSURL *r = [NSURL URLWithString:urlStr];
        
        [[UIApplication sharedApplication] openURL:r];
        
    }
    
    if ([str isEqualToString:@"搜狗地图"]) {
        
        NSString *urlStr = [NSString stringWithFormat:@"sgmap://map/routeplan?type=drive&fromcoord=%f,%f&tocoord=%f,%f&policy=1",[startLatitude floatValue],[startLongitude floatValue],destinationCoordinate.latitude,destinationCoordinate.longitude];
        
        NSURL *r = [NSURL URLWithString:urlStr];
        
        [[UIApplication sharedApplication] openURL:r];
        
    }
    
}

------ 注意 ------
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]
还有iOS9 是需要在plist文件配置白名单的LSApplicationQueriesSchemes
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,446评论 25 708
  • 因为要做一个地图操作的项目,需要用到这个地图库,但是查询官方API麻烦,而且这个地图框架的API做的用起来确实太麻...
    虚幻的锈色阅读 34,014评论 1 15
  • 吃货地图产品需求文档 V1.0-2015/03/30 1概述 1.1产品概述及目标 概述:“吃货地图”是一款基于i...
    michaelshan阅读 5,899评论 1 46
  • 最近在做导航,所以把自己找到的资料总结一下! 无论是百度地图、高德地图、谷歌地图还是腾讯地图它们都有自己的SDK,...
    misakatao阅读 7,693评论 2 23
  • 我期望我睡着了 命运就会有所改变 我躺进沉沉的黑夜 就像一粒 虔诚的种子 我期望天亮以后 我就长成了丰硕的粮食 太...
    Bohemian_e6af阅读 247评论 0 2