iOS--三方技术租房

百度地图、高德地图导航封装工具类

2016-07-26  本文已影响537人  袁俊亮技术博客

title : 百度地图、高德地图导航封装工具类
category : 常用封装


百度地图、高德地图导航封装工具类

标签(空格分隔): 常用封装


该类没有将百度地图和高德地图的导航集成到APP内部,仅仅只是做了一个跳转,当需要导航的时候,直接跳转到百度地图或高德地图的应用内进行导航。如果用户没有安装百度或高德地图,则直接跳转到AppStore让用户下载安装

.h文件


#import <UIKit/UIKit.h>

@interface JLNavTool : NSObject

/**
 *  点击后让用户选择是高德还是百度进行导航
 *
 *  @param latitude    当前位置的维度
 *  @param longitude   当前位置的经度
 *  @param toLatitude  目的地的维度
 *  @param toLongitude 目的地的经度
 *  @param toAddress   目的地的地址名称
 */
+ (void)beginNavWithClass:(UIViewController *)viewController LocationLatitude:(double)latitude longitude:(double)longitude toLatitude:(double)toLatitude toLongitude:(double)toLongitude toAddress:(NSString *)toAddress;

/**
 *  高德地图导航
 *
 *  @param latitude    当前位置的维度
 *  @param longitude   当前位置的经度
 *  @param toLatitude  目的地的维度
 *  @param toLongitude 目的地的经度
 *  @param toAddress   目的地的地址名称
 */
+ (void)gaodeNavWithLocationLatitude:(double)latitude longitude:(double)longitude toLatitude:(double)toLatitude toLongitude:(double)toLongitude toAddress:(NSString *)toAddress;
/**
 *  百度地图导航
 *
 *  @param latitude    当前位置的维度
 *  @param longitude   当前位置的经度
 *  @param toLatitude  目的地的维度
 *  @param toLongitude 目的地的经度
 *  @param toAddress   目的地的地址名称
 */
+ (void)baiduNavWithLocationLatitude:(double)latitude longitude:(double)longitude toLatitude:(double)toLatitude toLongitude:(double)toLongitude toAddress:(NSString *)toAddress;

@end

.m文件


#import "JLNavTool.h"

@implementation JLNavTool


/**
 *  选择是百度地图导航还是高德地图导航
 */
+ (void)beginNavWithClass:(UIViewController *)viewController LocationLatitude:(double)latitude longitude:(double)longitude toLatitude:(double)toLatitude toLongitude:(double)toLongitude toAddress:(NSString *)toAddress
{
    // 让用户选择使用哪个app
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"请选择所使用的导航工具" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //        [self baiduNav];
        [JLNavTool baiduNavWithLocationLatitude:latitude longitude:longitude toLatitude:toLatitude toLongitude:toLongitude toAddress:toAddress];
    }];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //        [self gaodeNav];
        [JLNavTool gaodeNavWithLocationLatitude:latitude longitude:longitude toLatitude:toLatitude toLongitude:toLongitude toAddress:toAddress];
    }];
    
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertVc addAction:action1];
    [alertVc addAction:action2];
    [alertVc addAction:action3];
    
    
    [viewController presentViewController:alertVc animated:YES completion:nil];
}


#pragma mark - 具体的地图导航
/**
 *  高德地图
 */
+ (void)gaodeNavWithLocationLatitude:(double)latitude longitude:(double)longitude toLatitude:(double)toLatitude toLongitude:(double)toLongitude toAddress:(NSString *)toAddress
{
    NSString * urlstr = [NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&backScheme=%@&sid=%@&slat=%.8f&slon=%.8f&sname=%@&did=%@&dlat=%.8f&dlon=%.8f&dname=%@&dev=%d&m=%d&t=%d",
                         @"豹修",              // 第三方应用程序名称
                         @"scheme",              // 第三方调回使用的 scheme,如 applicationScheme(第三方 iOS 客户端需要注册该 scheme)
                         @"StartPoint",             // 源ID
                         latitude,                  // 起点纬度,经纬度参数同时存在或同时为空,视为有效参数
                         longitude,                  // 起点经度,经纬度参数同时存在或同时为空,视为有效参数
                         @"我的位置",                // 起点名称
                         @"EndPoint",               // 目的 ID
                         toLatitude,               // 终点纬度,经纬度参数同时存在或同时为空,视为有效参数
                         toLongitude,               // 终点经度,经纬度参数同时存在或同时为空,视为有效参数
                         toAddress,          // 终点名称
                         0,                         // 起终点是否偏移(0:lat 和 lon 是已经加密后的,不需要国测加密; 1:需要国测加密) ,可为空,但起点或终点不为空时,不能为空
                         0,                         // 驾车:=0:速度最快,=1:费用最少,=2:距离最短,=3:不走高速,=4:躲避拥堵,=5:不走高速且避免收费,=6:不走高速且躲避拥堵,=7:躲避收费和拥堵,=8:不走高速躲避收费和拥堵 公交:0:最快捷,1:最经济,2:最少换乘,3:最少步行,4:最舒适,5:不乘地铁
                         1];                        // t = 0(驾车) =1(公交)=2(步行)
    
    urlstr = [urlstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *urlgaode = [NSURL URLWithString:urlstr];
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        [[UIApplication sharedApplication] openURL:urlgaode];
    }else{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/cn/app//id461703208?mt=8"]];
    }
}


/**
 *  百度地图
 */
+ (void)baiduNavWithLocationLatitude:(double)latitude longitude:(double)longitude toLatitude:(double)toLatitude toLongitude:(double)toLongitude toAddress:(NSString *)toAddress
{
    
    // 百度地图
    NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02", toLatitude, toLongitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }else{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/us/app/bai-du-tu-shou-ji-tu-lu-xian/id452186370?mt=8"]];
    }
}

@end

有时候我们获取的定位和我们想要的定位有较大的偏差,这时候我们可能需要转换坐标到统一的定位坐标系中。具体的转换方法可参考另一篇文章
iOS地图上WGS84、GCJ-02、BD-09互转解决方案

上一篇 下一篇

猜你喜欢

热点阅读