美文网首页iOS大咖说iOS常用地图开发相关
iOS App内部调起百度地图、高德地图、腾讯地图

iOS App内部调起百度地图、高德地图、腾讯地图

作者: iOS小武哥 | 来源:发表于2021-02-03 17:10 被阅读0次

公司App集成的是百度地图,然后调起百度地图、高德地图、腾讯地图、苹果地图进行导航的功能.

一.首先需要在info.plist文件中添加白名单如下:
<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>baidumap</string> //百度
        <string>iosamap</string> //高德
        <string>qqmap</string> //腾讯
    </array>

使用: [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"白名单://"]] 判断是否安装白名单里面的App.

二.由于项目继承的是百度地图,调用其他导航会有地理偏差,我们要进行坐标转换,可以使用这个转换工具.
第一种:调起苹果自带的地图进行导航:

苹果自带的地图不需要设置白名单,需要设置终点的经纬度就行,需要导入#import <MapKit/MapKit.h>头文件

//坐标转换
CLLocationCoordinate2D gcj02Coord = [JZLocationConverter bd09ToGcj02:loc];
    //用户位置
    MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
    //终点位置
    MKMapItem *toLocation = [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:gcj02Coord addressDictionary:nil] ];
    toLocation.name = title;
    
    NSArray *items = @[currentLoc,toLocation];
    NSDictionary *dic = @{
                          MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
                          MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
                          MKLaunchOptionsShowsTrafficKey : @(YES)
                          };
    [MKMapItem openMapsWithItems:items launchOptions:dic];
第二种:调起百度地图进行导航:

可以参考:百度地图官方文档 实现如下:

//百度地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name:%@&mode=driving&coord_type=bd09ll",modelData.coordinate.latitude,modelData.coordinate.longitude, modelData.titleName] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
       [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: urlString]];
    }

参数设置可以参考文档

第三种:调起高德地图进行导航:

可以参考:高德地图官方文档 实现如下:

CLLocationCoordinate2D gcj02Coord = [JZLocationConverter bd09ToGcj02:modelData.coordinate];

    float shopLat = gcj02Coord.latitude;
    float shoplng = gcj02Coord.longitude;
    
    //高德地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];
        NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&backScheme=%@&sname=我的位置&dev=1&t=0&dlat=%f&dlon=%f&dname=%@&dev=0",@"AppName",@"url schemes",shopLat,shoplng, modelData.titleName] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
       [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: urlString]];  
    }
第四种:调起腾讯地图进行导航:

可以参考:腾讯地图官方文档 实现如下:

//腾讯地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
        NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];
        NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%f,%f&to=%@",shopLat,shoplng, modelData.titleName] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
       [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: urlString]]; 
    }
下面是地理坐标转换工具:

.h

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface LocationConverter : NSObject

/**
 *  @brief  世界标准地理坐标(WGS-84) 转换成 中国国测局地理坐标(GCJ-02)<火星坐标>
 *
 *  ####只在中国大陆的范围的坐标有效,以外直接返回世界标准坐标
 *
 *  @param  location    世界标准地理坐标(WGS-84)
 *
 *  @return 中国国测局地理坐标(GCJ-02)<火星坐标>
 */
+ (CLLocationCoordinate2D)wgs84ToGcj02:(CLLocationCoordinate2D)location;


/**
 *  @brief  中国国测局地理坐标(GCJ-02) 转换成 世界标准地理坐标(WGS-84)
 *
 *  ####此接口有1-2米左右的误差,需要精确定位情景慎用
 *
 *  @param  location    中国国测局地理坐标(GCJ-02)
 *
 *  @return 世界标准地理坐标(WGS-84)
 */
+ (CLLocationCoordinate2D)gcj02ToWgs84:(CLLocationCoordinate2D)location;


/**
 *  @brief  世界标准地理坐标(WGS-84) 转换成 百度地理坐标(BD-09)
 *
 *  @param  location    世界标准地理坐标(WGS-84)
 *
 *  @return 百度地理坐标(BD-09)
 */
+ (CLLocationCoordinate2D)wgs84ToBd09:(CLLocationCoordinate2D)location;


/**
 *  @brief  中国国测局地理坐标(GCJ-02)<火星坐标> 转换成 百度地理坐标(BD-09)
 *
 *  @param  location    中国国测局地理坐标(GCJ-02)<火星坐标>
 *
 *  @return 百度地理坐标(BD-09)
 */
+ (CLLocationCoordinate2D)gcj02ToBd09:(CLLocationCoordinate2D)location;


/**
 *  @brief  百度地理坐标(BD-09) 转换成 中国国测局地理坐标(GCJ-02)<火星坐标>
 *
 *  @param  location    百度地理坐标(BD-09)
 *
 *  @return 中国国测局地理坐标(GCJ-02)<火星坐标>
 */
+ (CLLocationCoordinate2D)bd09ToGcj02:(CLLocationCoordinate2D)location;


/**
 *  @brief  百度地理坐标(BD-09) 转换成 世界标准地理坐标(WGS-84)
 *
 *  ####此接口有1-2米左右的误差,需要精确定位情景慎用
 *
 *  @param  location    百度地理坐标(BD-09)
 *
 *  @return 世界标准地理坐标(WGS-84)
 */
+ (CLLocationCoordinate2D)bd09ToWgs84:(CLLocationCoordinate2D)location;

@end

.m

#import "LocationConverter.h"
#import <CoreLocation/CoreLocation.h>
#define LAT_OFFSET_0(x,y) -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(x))
#define LAT_OFFSET_1 (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0
#define LAT_OFFSET_2 (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0
#define LAT_OFFSET_3 (160.0 * sin(y / 12.0 * M_PI) + 320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0

#define LON_OFFSET_0(x,y) 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(x))
#define LON_OFFSET_1 (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0
#define LON_OFFSET_2 (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0
#define LON_OFFSET_3 (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0

#define RANGE_LON_MAX 137.8347
#define RANGE_LON_MIN 72.004
#define RANGE_LAT_MAX 55.8271
#define RANGE_LAT_MIN 0.8293
// jzA = 6378245.0, 1/f = 298.3
// b = a * (1 - f)
// ee = (a^2 - b^2) / a^2;
#define jzA 6378245.0
#define jzEE 0.00669342162296594323



@implementation LocationConverter

+ (double)transformLat:(double)x bdLon:(double)y
{
    double ret = LAT_OFFSET_0(x, y);
    ret += LAT_OFFSET_1;
    ret += LAT_OFFSET_2;
    ret += LAT_OFFSET_3;
    return ret;
}

+ (double)transformLon:(double)x bdLon:(double)y
{
    double ret = LON_OFFSET_0(x, y);
    ret += LON_OFFSET_1;
    ret += LON_OFFSET_2;
    ret += LON_OFFSET_3;
    return ret;
}

+ (BOOL)outOfChina:(double)lat bdLon:(double)lon
{
    if (lon < RANGE_LON_MIN || lon > RANGE_LON_MAX)
        return true;
    if (lat < RANGE_LAT_MIN || lat > RANGE_LAT_MAX)
        return true;
    return false;
}

+ (CLLocationCoordinate2D)gcj02Encrypt:(double)ggLat bdLon:(double)ggLon
{
    CLLocationCoordinate2D resPoint;
    double mgLat;
    double mgLon;
    if ([self outOfChina:ggLat bdLon:ggLon]) {
        resPoint.latitude = ggLat;
        resPoint.longitude = ggLon;
        return resPoint;
    }
    double dLat = [self transformLat:(ggLon - 105.0)bdLon:(ggLat - 35.0)];
    double dLon = [self transformLon:(ggLon - 105.0) bdLon:(ggLat - 35.0)];
    double radLat = ggLat / 180.0 * M_PI;
    double magic = sin(radLat);
    magic = 1 - jzEE * magic * magic;
    double sqrtMagic = sqrt(magic);
    dLat = (dLat * 180.0) / ((jzA * (1 - jzEE)) / (magic * sqrtMagic) * M_PI);
    dLon = (dLon * 180.0) / (jzA / sqrtMagic * cos(radLat) * M_PI);
    mgLat = ggLat + dLat;
    mgLon = ggLon + dLon;
    
    resPoint.latitude = mgLat;
    resPoint.longitude = mgLon;
    return resPoint;
}

+ (CLLocationCoordinate2D)gcj02Decrypt:(double)gjLat gjLon:(double)gjLon {
    CLLocationCoordinate2D  gPt = [self gcj02Encrypt:gjLat bdLon:gjLon];
    double dLon = gPt.longitude - gjLon;
    double dLat = gPt.latitude - gjLat;
    CLLocationCoordinate2D pt;
    pt.latitude = gjLat - dLat;
    pt.longitude = gjLon - dLon;
    return pt;
}

+ (CLLocationCoordinate2D)bd09Decrypt:(double)bdLat bdLon:(double)bdLon
{
    CLLocationCoordinate2D gcjPt;
    double x = bdLon - 0.0065, y = bdLat - 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);
    gcjPt.longitude = z * cos(theta);
    gcjPt.latitude = z * sin(theta);
    return gcjPt;
}

+(CLLocationCoordinate2D)bd09Encrypt:(double)ggLat bdLon:(double)ggLon
{
    CLLocationCoordinate2D bdPt;
    double x = ggLon, y = ggLat;
    double z = sqrt(x * x + y * y) + 0.00002 * sin(y * M_PI);
    double theta = atan2(y, x) + 0.000003 * cos(x * M_PI);
    bdPt.longitude = z * cos(theta) + 0.0065;
    bdPt.latitude = z * sin(theta) + 0.006;
    return bdPt;
}


+ (CLLocationCoordinate2D)wgs84ToGcj02:(CLLocationCoordinate2D)location
{
    return [self gcj02Encrypt:location.latitude bdLon:location.longitude];
}

+ (CLLocationCoordinate2D)gcj02ToWgs84:(CLLocationCoordinate2D)location
{
    return [self gcj02Decrypt:location.latitude gjLon:location.longitude];
}


+ (CLLocationCoordinate2D)wgs84ToBd09:(CLLocationCoordinate2D)location
{
    CLLocationCoordinate2D gcj02Pt = [self gcj02Encrypt:location.latitude
                                                  bdLon:location.longitude];
    return [self bd09Encrypt:gcj02Pt.latitude bdLon:gcj02Pt.longitude] ;
}

+ (CLLocationCoordinate2D)gcj02ToBd09:(CLLocationCoordinate2D)location
{
    return  [self bd09Encrypt:location.latitude bdLon:location.longitude];
}

+ (CLLocationCoordinate2D)bd09ToGcj02:(CLLocationCoordinate2D)location
{
    return [self bd09Decrypt:location.latitude bdLon:location.longitude];
}

+ (CLLocationCoordinate2D)bd09ToWgs84:(CLLocationCoordinate2D)location
{
    CLLocationCoordinate2D gcj02 = [self bd09ToGcj02:location];
    return [self gcj02Decrypt:gcj02.latitude gjLon:gcj02.longitude];
}

@end

相关文章

  • RN-地图导航

    调起百度网页地图路径导航 调起高德网页地图路径导航 iOS调起百度APP地图路径导航 iOS调起高德app地图路径...

  • iOS App内部调起百度地图、高德地图、腾讯地图

    公司App集成的是百度地图,然后调起百度地图、高德地图、腾讯地图、苹果地图进行导航的功能. 一.首先需要在info...

  • h5调用地图功能

    h5 调起百度、高德地图定位 高德地图 百度地图 注意: 高德地图和百度地图的经纬度传值是相反着的 h5 调起高...

  • IOS 调用地图

    App中如何打开百度或者高德地图 百度地图URI API地址高德地图URI API地址腾讯地图URI API地址 ...

  • 地图集成调研

    地图集成调研 主要地图API有百度地图、高德地图、腾讯地图、搜狗地图(android、IOS暂不开放)。 百度地图...

  • js唤醒手机百度地图高德地图app

    高德地图 百度地图 高德地图和百度地图的经纬度传值是相反着的 调起百度地图pc或者web api

  • 百度地图

    IOS第三方地图 百度地图高德地图腾讯地图苹果自带地图谷歌地图(中国屏蔽了) 接下来我们说一下百度地图百度地图SD...

  • iOS 跳转地图(谷歌,高德,百度,腾讯,苹果)

    功能包括跳转谷歌地图,高德地图,百度地图,腾讯地图,苹果地图其中 设置URL Scheme(iOS11以上左上角可...

  • Flutter(Dart)经纬度坐标系转换

    Flutter(Dart)经纬度坐标系转换iOS 调起地图App进行导航(百度,高德,系统自带高德)URL API方式

  • 高的地图

    分享 高德地图的使用 地图产品有: 谷歌 百度 高德 腾讯 ArcGis 超图 天地图 ...

网友评论

    本文标题:iOS App内部调起百度地图、高德地图、腾讯地图

    本文链接:https://www.haomeiwen.com/subject/lkkmtltx.html