app调用百度地图/高德地图/腾讯地图,地图SDK收费了!!!

2,786 阅读4分钟

app内置sdk想必大家都看到了,自从2021年12月份三家主流地图sdk几乎同事宣布商用收费,应用到的地图展示,地图定位,导航等SDK功能都是需要收费的(公益app除外),不管你是大型的企业还是小企业,不管你是访问一次还是几百万次,一刀切一视同仁,在这种情况下好多公司,应用功能不多的情况下都是选择删除相应地图SDK,展示地图图片或者仅仅是展示地址,自行搜索.在这种情况下,使用app跳转调用也许会是一个比较缓和的方式,基于这种情况,列举一下使用情况.

三家的调用app文档如下:

百度地图

高德地图

腾讯地图

以下为路线规划/标注处理

由于地图执行的标准不一样,定位坐标需要转换,如下:

//百度转火星坐标

+ (CLLocationCoordinate2D )bdToGGEncrypt:(CLLocationCoordinate2D)coord

{
    double x = coord.longitude - 0.0065, y = coord.latitude - 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);
    CLLocationCoordinate2D transformLocation ;
    transformLocation.longitude = z * cos(theta);
    transformLocation.latitude = z * sin(theta);
    return transformLocation;
}


//火星坐标转百度坐标

+ (CLLocationCoordinate2D )ggToBDEncrypt:(CLLocationCoordinate2D)coord

{
    double x = coord.longitude, y = coord.latitude;
    double z = sqrt(x * x + y * y) + 0.00002 * sin(y * M_PI);
    double theta = atan2(y, x) + 0.000003 * cos(x * M_PI);
    CLLocationCoordinate2D transformLocation ;
    transformLocation.longitude = z * cos(theta) + 0.0065;
    transformLocation.latitude = z * sin(theta) + 0.006;
    return transformLocation;

}

xcode中注意事项:

1.添加配置白名单

image.png

2.保证添加的键值要在前50的位置不然判断地图app是否安装,一直会返回NO

判断是否安装:
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {

    }
    //高德地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {

    }
    //腾讯地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {

    }

展示路线规划:


苹果地图:
    NSDictionary *options = @{
                            MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
                            MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
                            MKLaunchOptionsShowsTrafficKey : @(YES)
    };

    MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
    //如果存在经纬度,是进行起点/终点传入处理
    if (latitude.length) {

        CLLocationCoordinate2D loc = CLLocationCoordinate2DMake([latitude doubleValue], [longitude doubleValue]);
        MKMapItem *toLocation = [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:loc addressDictionary:nil] ];
        toLocation.name = query;
        [MKMapItem openMapsWithItems:@[currentLoc,toLocation] launchOptions:options];
    }
    //如果没有经纬度,检索地点名称
    else{

        CLGeocoder *geocoder = [CLGeocoder new];
        [geocoder geocodeAddressString:query completionHandler:^(NSArray *placemarks, NSError *error) {

            CLPlacemark *clToPlacemark=[placemarks firstObject];//获取第一个地标
            MKPlacemark *mkToPlacemark=[[MKPlacemark alloc]initWithPlacemark:clToPlacemark];
            MKMapItem *toLocation=[[MKMapItem alloc]initWithPlacemark:mkToPlacemark];
            toLocation.name = query;
            [MKMapItem openMapsWithItems:@[currentLoc,toLocation] launchOptions:options];
        }];
    }
        
百度地图:
    //存在经纬度为线路规划
    if (latitude.length > 0) {

        urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=我的位置&destination=latlng:%@,%@|name:%@&mode=driving&coord_type=gcj02&src=ios.yourCompanyName.yourAppName",latitude,longitude,address];
    }
    //不存在经纬度为只传destination:即可
    else{

        urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=我的位置&destination=%@&mode=driving&coord_type=gcj02&src=ios.yourCompanyName.yourAppName",address];
    }
    
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL * mapUrl = [NSURL URLWithString:urlString];
    
    if ([[UIApplication sharedApplication] canOpenURL:mapUrl]) {

        [[UIApplication sharedApplication] openURL:mapUrl];
    }
    
高德地图:
    if (latitude.length > 0) {

    urlString = [NSString stringWithFormat:@"iosamap://path?sourceApplication=yourAppName&sid=&slat=&slon=&sname=&did=&dlat=%@&dlon=%@&dname=%@&dev=0&t=0",baseLatitude,baseLongitude,address];
    }else{
    
    urlString = [NSString stringWithFormat:@"iosamap://path?sourceApplication=yourAppName&sid=&slat=&slon=&sname=&did=&dlat=&dlon=&dname=%@&dev=0&t=0",address];
    }

    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL * mapUrl = [NSURL URLWithString:urlString];
    
    if ([[UIApplication sharedApplication] canOpenURL:mapUrl]) {

        [[UIApplication sharedApplication] openURL:mapUrl];
    }
    
腾讯地图:
if (latitude.length > 0) {

    urlString = [NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&from=我的位置&fromcoord=CurrentLocation&to=%@&tocoord=%@,%@",address,baseLatitude,baseLongitude];
    }else{
    
    urlString = [NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&from=我的位置&fromcoord=CurrentLocation&to=%@&tocoord=",address];
    }

    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL * mapUrl = [NSURL URLWithString:urlString];
    
    if ([[UIApplication sharedApplication] canOpenURL:mapUrl]) {

        [[UIApplication sharedApplication] openURL:mapUrl];
    }

展示地图标注:

注意:如无坐标,只能是进行搜索/地址解析,这样更准确一些,有经纬度为mark标注类型

苹果地图:

    if (latitude.length > 0) {

        CLLocationCoordinate2D loc = CLLocationCoordinate2DMake([latitude doubleValue], [longitude doubleValue]);
        MKPlacemark * mkPlacemark= [[MKPlacemark alloc]initWithCoordinate:loc addressDictionary:nil];
        MKMapItem * markLocation = [[MKMapItem alloc]initWithPlacemark:mkPlacemark];
        markLocation.name = query;
        [MKMapItem openMapsWithItems:@[markLocation] launchOptions:nil];

     }else{

        CLGeocoder *geocoder = [CLGeocoder new];
        [geocoder geocodeAddressString:query completionHandler:^(NSArray *placemarks, NSError *error) {

            CLPlacemark *clPlacemark=[placemarks firstObject];//获取第一个地标
            MKPlacemark * mkPlacemark=[[MKPlacemark alloc]initWithPlacemark:clPlacemark];
            MKMapItem * markLocation=[[MKMapItem alloc]initWithPlacemark:mkPlacemark];
            markLocation.name = query;
            [MKMapItem openMapsWithItems:@[markLocation] launchOptions:nil];
        }];
    }
    
    百度地图:
    if (latitude.length > 0) {

        urlString = [NSString stringWithFormat:@"baidumap://map/marker?location=%@,%@address=%@&title=%@&content=%@&src=ios.yourCompanyName.yourAppName",baseLatitude,baseLongitude,address,address,address];
    }
    else{

        urlString = [NSString stringWithFormat:@"baidumap://map/geocoder?address=%@&src=ios.yourCompanyName.yourAppName",address];
    }
    
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL * mapUrl = [NSURL URLWithString:urlString];
    
    if ([[UIApplication sharedApplication] canOpenURL:mapUrl]) {

        [[UIApplication sharedApplication] openURL:mapUrl];
    }
    
高德地图:
    if (latitude.length > 0) {

        urlString = [NSString stringWithFormat:@"iosamap://viewMap?sourceApplication=yourAppName&poiname=%@&lat=%@&lon=%@&dev=0",address,baseLatitude,baseLongitude];
    }else{
    
        urlString = [NSString stringWithFormat:@"iosamap://poi?sourceApplication=yourAppName&name=%@&lat1=&lon1=&lat2=&lon2=&dev=0",address];
    }

    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL * mapUrl = [NSURL URLWithString:urlString];
    
    if ([[UIApplication sharedApplication] canOpenURL:mapUrl]) {

        [[UIApplication sharedApplication] openURL:mapUrl];
    }
    
腾讯地图:
    if (latitude.length > 0) {

        urlString = [NSString stringWithFormat:@"qqmap://map/marker?marker=coord:%@,%@;title:%@;addr:%@",baseLatitude,baseLongitude,address,address];
    }else{
    
        urlString = [NSString stringWithFormat:@"qqmap://map/search?keyword=%@",address];
    }

    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL * mapUrl = [NSURL URLWithString:urlString];
    
    if ([[UIApplication sharedApplication] canOpenURL:mapUrl]) {

        [[UIApplication sharedApplication] openURL:mapUrl];
    }
    

这样就是我们常用的展示地图标注和地图路线规划,调用地图百度/高德app时候现在需要传app的名称/公司名称等,腾讯需要传appkey(但是我没传也能调用),文档说是为了统计,后面会不会统一需要使用appkey来卡住大家,让大家使用SDK进行收费展示,或者跳转也需要收费,不得而知,目前这是个人认为比较好的临时解决办法,避免让用户再次搜索导航等操作.如有不妥之处,请大家提醒一下!!