美文网首页iOS常用
iOS 通过openURL打开本地地图(及不同坐标系转化)

iOS 通过openURL打开本地地图(及不同坐标系转化)

作者: 冰点雨 | 来源:发表于2020-05-15 10:30 被阅读0次

    在调地图之前,先了解下不同地图使用的坐标系

    API 坐标系
    百度地图API 百度坐标
    腾讯搜搜地图API 火星坐标
    搜狐搜狗地图API 搜狗坐标
    阿里云地图API 火星坐标
    图吧MapBar地图API 图吧坐标
    高德MapABC地图API 火星坐标
    灵图51ditu地图API 火星坐标

    openURL打开地图

    打开本地地图,看用户安装了哪些地图APP

    已经安装的,弹窗选择。没有安装,就调起苹果地图。

     NSMutableArray *mapArr = [[NSMutableArray alloc]init];
        if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]){
            [mapArr addObject:@"百度地图"];
        }
        if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
            [mapArr addObject:@"高德地图"];
        }
        if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]){
               [mapArr addObject:@"腾讯地图"];
           }
        [mapArr addObject:@"苹果地图"];
        
        if (mapArr.count == 1) {
            [self jumpToMap:mapArr[0]];
        }
        
        KWeakSelf(self);
        WMZDialog *alert = Dialog();
        alert
        .wTypeSet(DialogTypeSheet)
        .wDataSet(mapArr)
        .wTitleSet(@"请选择")
        .wCancelColorSet(RGB(240, 61, 56))
        .wMainRadiusSet(0)
        .wMainBackColorSet(RGB(242, 242, 242))
        .wTitleFontSet(15)
        .wCancelFontSet(15)
        .wMessageFontSet(15)
        .wTitleColorSet(RGB(51, 51, 51))
        .wLineColorSet(RGB(242, 242, 242))
        .wMessageColorSet(RGB(51, 51, 51))
        //完成操作事件
        .wEventFinishSet(^(id anyID,NSIndexPath *path, DialogType type) {
            NSLog(@"%@",anyID);
    
            [alert closeView];
            [weakself jumpToMap:anyID];
            
           
        })
        .wStart();
    }
    
    - (void)jumpToMap:(NSString *)mapName{
        if ([mapName isEqualToString:@"高德地图"])//高德地图
        {
            [self openMapGaoDe];
        }
        else if ([mapName isEqualToString:@"百度地图"])//百度地图
        {
            [self openMapBaiDu];
        }
        else if ([mapName isEqualToString:@"腾讯地图"])//腾讯地图
        {
            [self openMapQQ];
        }
        else//苹果地图
        {
            [self openMapApple];
        }
    }
    

    百度地图

    百度地图官方文档

    注意:我定位使用的是腾讯定位。腾讯用的是火星坐标 ,而百度用的是百度坐标 使用的时候需要转化

    //百度地图
    - (void)openMapBaiDu{
     
        CLLocation *coor = [[CLLocation alloc]initWithLatitude:[self.model.lat doubleValue] longitude:[self.model.lng doubleValue]];
        // 转为百度经纬度类型的坐标
        CLLocation *location = [coor locationBaiduFromMars];
        
        NSString *urlStr = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=name:%@|latlng:%f,%f&coord_type=bd09ll&mode=walking&src=ios.wkbb.%@",self.model.simple_name,location.coordinate.latitude,location.coordinate.longitude,APP_NAME] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlStr] options:@{} completionHandler:^(BOOL success) {
                   
        }];
    

    高德地图

    高德地图官方文档

    //高德地图
    - (void)openMapGaoDe{
    
        NSString *urlStr = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=APPName&sid=&slat=&slon=&sname=我的位置&did=&dlat=%@&dlon=%@&dname=%@&dev=0&t=0",self.model.lat,self.model.lng,self.model.simple_name] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
           if (@available(iOS 10.0, *)) {
               [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlStr] options:@{} completionHandler:^(BOOL success) {
                   
               }];
           } else {
               // Fallback on earlier versions
               [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlStr]];
           }
    }
    

    腾讯地图

    腾讯地图官方文档

    NSString *urlStr = [[NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&from=我的位置&fromcoord=&to=%@&tocoord=%@,%@&referer=%@",self.model.simple_name,self.model.lat,self.model.lng,@"腾讯key"] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
           if (@available(iOS 10.0, *)) {
               [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlStr] options:@{} completionHandler:^(BOOL success) {
                   
               }];
           } else {
               // Fallback on earlier versions
               [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlStr]];
           }
    

    苹果地图

    1、添加库MapKit.framework
    2、引入: #import <MapKit/MapKit.h>

    //苹果地图
    - (void)openMapApple{
        
        //终点坐标
        CLLocationCoordinate2D desCoordinate = CLLocationCoordinate2DMake([self.model.lat doubleValue], [self.model.lng doubleValue]);
        //用户位置
        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
        currentLocation.name = @"我的位置";
        //终点位置
        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:desCoordinate addressDictionary:nil]];
        toLocation.name = self.model.address;
        [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
                                  launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeTransit,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
    }
    

    不同坐标转化

    新建一个CLLocation的类别

    CLLocation+JRLocation.h

    //  火星坐标系转换扩展
    
    
    
    
    /*
    从 CLLocationManager 取出来的经纬度放到 mapView 上显示,是错误的!
    从 CLLocationManager 取出来的经纬度去 Google Maps API 做逆地址解析,当然是错的!
    从 MKMapView 取出来的经纬度去 Google Maps API 做逆地址解析终于对了。去百度地图API做逆地址解析,依旧是错的!
    从上面两处取的经纬度放到百度地图上显示都是错的!错的!的!
     
    分为 地球坐标,火星坐标(iOS mapView 高德 , 国内google ,搜搜、阿里云 都是火星坐标),百度坐标(百度地图数据主要都是四维图新提供的)
     
    火星坐标: MKMapView
    地球坐标: CLLocationManager
     
    当用到CLLocationManager 得到的数据转化为火星坐标, MKMapView不用处理
     
     
    API                坐标系
    百度地图API         百度坐标
    腾讯搜搜地图API      火星坐标
    搜狐搜狗地图API      搜狗坐标
    阿里云地图API       火星坐标
    图吧MapBar地图API   图吧坐标
    高德MapABC地图API   火星坐标
    灵图51ditu地图API   火星坐标
     
    */
    
    #import <CoreLocation/CoreLocation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface CLLocation (JRLocation)
    
    
    //从地图坐标转化到火星坐标
    - (CLLocation*)locationMarsFromEarth;
      
    //从火星坐标转化到百度坐标
    - (CLLocation*)locationBaiduFromMars;
      
    //从百度坐标到火星坐标
    - (CLLocation*)locationMarsFromBaidu;
      
    //从火星坐标到地图坐标
    //- (CLLocation*)locationEarthFromMars; // 未实现
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    

    CLLocation+JRLocation.m

    //
    //  CLLocation+JRLocation.m
    
    
    #import "CLLocation+JRLocation.h"
    
    void transform_earth_from_mars(double lat, double lng, double* tarLat, double* tarLng);
    void transform_mars_from_baidu(double lat, double lng, double* tarLat, double* tarLng);
    void transform_baidu_from_mars(double lat, double lng, double* tarLat, double* tarLng);
    
    
    @implementation CLLocation (JRLocation)
    
    //从地图坐标转化到火星坐标
    - (CLLocation*)locationMarsFromEarth
    {
        double lat = 0.0;
        double lng = 0.0;
        transform_earth_from_mars(self.coordinate.latitude, self.coordinate.longitude, &lat, &lng);
        return [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(lat, lng)
                                             altitude:self.altitude
                                   horizontalAccuracy:self.horizontalAccuracy
                                     verticalAccuracy:self.verticalAccuracy
                                               course:self.course
                                                speed:self.speed
                                            timestamp:self.timestamp];
    }
      
    
    
      
    //从火星坐标转化到百度坐标
    - (CLLocation*)locationBaiduFromMars
    {
        double lat = 0.0;
        double lng = 0.0;
        transform_mars_from_baidu(self.coordinate.latitude, self.coordinate.longitude, &lat, &lng);
        return [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(lat, lng)
                                             altitude:self.altitude
                                   horizontalAccuracy:self.horizontalAccuracy
                                     verticalAccuracy:self.verticalAccuracy
                                               course:self.course
                                                speed:self.speed
                                            timestamp:self.timestamp];
    }
      
    //从百度坐标到火星坐标
    - (CLLocation*)locationMarsFromBaidu
    {
        double lat = 0.0;
        double lng = 0.0;
        transform_baidu_from_mars(self.coordinate.latitude, self.coordinate.longitude, &lat, &lng);
        return [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(lat, lng)
                                             altitude:self.altitude
                                   horizontalAccuracy:self.horizontalAccuracy
                                     verticalAccuracy:self.verticalAccuracy
                                               course:self.course
                                                speed:self.speed
                                            timestamp:self.timestamp];
    }
    
    
    - (CLLocation*)locationEarthFromMars
    {
        // 二分法查纠偏文件
        // http://xcodev.com/131.html
        return nil;
    }
    
    @end
    
    
    
    
    // --- transform_earth_from_mars ---
    // 参考来源:https://on4wp7.codeplex.com/SourceControl/changeset/view/21483#353936
    // Krasovsky 1940
    //
    // a = 6378245.0, 1/f = 298.3
    // b = a * (1 - f)
    // ee = (a^2 - b^2) / a^2;
    const double a = 6378245.0;
    const double ee = 0.00669342162296594323;
     
    bool transform_sino_out_china(double lat, double lon)
    {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    }
     
    double transform_earth_from_mars_lat(double x, double y)
    {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x));
        ret += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
        ret += (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0;
        ret += (160.0 * sin(y / 12.0 * M_PI) + 320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }
     
    double transform_earth_from_mars_lng(double x, double y)
    {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));
        ret += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
        ret += (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0;
        ret += (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0;
        return ret;
    }
     
    void transform_earth_from_mars(double lat, double lng, double* tarLat, double* tarLng)
    {
        if (transform_sino_out_china(lat, lng))
        {
            *tarLat = lat;
            *tarLng = lng;
            return;
        }
        double dLat = transform_earth_from_mars_lat(lng - 105.0, lat - 35.0);
        double dLon = transform_earth_from_mars_lng(lng - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * M_PI;
        double magic = sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * M_PI);
        dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * M_PI);
        *tarLat = lat + dLat;
        *tarLng = lng + dLon;
    }
     
    // --- transform_earth_from_mars end ---
    // --- transform_mars_vs_bear_paw ---
    // 参考来源:http://blog.woodbunny.com/post-68.html
    const double x_pi = M_PI * 3000.0 / 180.0;
     
    void transform_mars_from_baidu(double gg_lat, double gg_lon, double *bd_lat, double *bd_lon)
    {
        double x = gg_lon, y = gg_lat;
        double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
        double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
        *bd_lon = z * cos(theta) + 0.0065;
        *bd_lat = z * sin(theta) + 0.006;
    }
     
    void transform_baidu_from_mars(double bd_lat, double bd_lon, double *gg_lat, double *gg_lon)
    {
        double x = bd_lon - 0.0065, y = bd_lat - 0.006;
        double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
        double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
        *gg_lon = z * cos(theta);
        *gg_lat = z * sin(theta);
    }
    
    

    相关文章

      网友评论

        本文标题:iOS 通过openURL打开本地地图(及不同坐标系转化)

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