zoukankan      html  css  js  c++  java
  • ios 地图的坐标系转换 调用手机的系统及三方地图

    坐标转换

    一、坐标简介
    目前国内主流坐标系类型主要有三种:WGS84、GCJ02、BD09;
    1.WGS84:为一种大地坐标系,也是目前广泛使用的GPS全球卫星定位系统使用的坐标系,如:iOS系统自带地图坐标。

    1. GCJ02:是由中国国家测绘局制订的地理信息系统的坐标系统,是由WGS84坐标系经加密后的坐标系,如:高德地图、google地图、soso地图、aliyun地图、mapabc地图和amap地图所用坐标
    2. BD09:百度坐标系,在GCJ02坐标系基础上再次加密。其中BD09ll表示百度经纬度坐标,BD09mc表示百度墨卡托米制坐标,如:百度地图所用坐标

    二、一般项目集成的sdk都支持别的坐标转成自己的坐标,如百度地图支持WGS84和GCJ02转BD09ll,也支持BD09ll转GCJ02,但是color{red}{不支持BD09ll转WGS84},下面就主要介绍一下GCJ02和BD09ll转WGS84

    二、BD09ll转GCJ02

    /**
     百度坐标转高德坐标
    
     @param glat 纬度
     @param glon 经度
     @return 数组【转换后的纬度,转换后的经度】
     */
    + (NSArray *)bd09llToGCJLat:(double)glat lon:(double)glon {
        double X_PI = M_PI * 3000.0 / 180.0;
        double x = glon - 0.0065;
        double y = glat - 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);
        double lat = z * sin(theta);
        double lon = z * cos(theta);
        NSArray *latlon = @[[NSNumber numberWithDouble:lat],[NSNumber numberWithDouble:lon]];
        
        return latlon;
    }
    

    三、GCJ02转WGS84(只支持国内)

    /**
     高德坐标转系统自带坐标
    
     @param array 数组 需要转换的坐标【纬度,经度】
     @return 数组【转换后的纬度,转换后的经度】
     */
    + (NSArray *)gcjToWGS:(NSArray *)array {
        double lat = [array.firstObject doubleValue];
        double lon = [array.lastObject doubleValue];
        if ([self outOfChinalat:lat lon:lon]){
            return @[[NSNumber numberWithDouble:lat],[NSNumber numberWithDouble:lon]];
        }
        NSArray *latlon = [self deltalat:lat lon:lon];
        return latlon;
    }
    
    + (BOOL)outOfChinalat:(double)lat lon:(double)lon {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    }
    
    + (NSArray *)deltalat:(double)wgLat lon:(double)wgLon {
        double OFFSET = 0.00669342162296594323;
        double AXIS = 6378245.0;
        
        double dLat = [self transformLatx:(wgLon - 105.0) lon:(wgLat - 35.0)];
        double dLon = [self transformLonx:(wgLon - 105.0) lon:(wgLat - 35.0)];
        double radLat = wgLat / 180.0 * M_PI;
        double magic = sin(radLat);
        magic = 1 - OFFSET * magic * magic;
        double sqrtMagic = sqrt(magic);
        dLat = (dLat * 180.0) / ((AXIS * (1 - OFFSET)) / (magic * sqrtMagic) * M_PI);
        dLon = (dLon * 180.0) / (AXIS / sqrtMagic * cos(radLat) * M_PI);
        return @[[NSNumber numberWithDouble:(wgLat - dLat)],[NSNumber numberWithDouble:(wgLon - dLon)]];
    }
    
    + (double)transformLatx:(double)x lon:(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)transformLonx:(double)x lon:(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;
    }
    

    四、BD09ll转WGS84,目前的做法是先将BD09ll坐标转成GCJ02坐标,然后再转成WGS84坐标。

    五、经实测,转换后的坐标color{red}{误差小与1米},另GCJ02坐标转WGS84坐标有一个更精确的算法,不过里面设计到了递归。



    ------------------------------打开地图的方式---------------------
       
     

    //手机自带地图

        //当前位置

        MKMapItem *mylocation = [MKMapItemmapItemForCurrentLocation];

        //前面填写纬度

        CLLocationCoordinate2D coords2 =CLLocationCoordinate2DMake(cell.model.coordinate.latitude, cell.model.coordinate.longitude);

        //目的地的位置

        MKMapItem *toLocation = [[MKMapItemalloc] initWithPlacemark:[[MKPlacemarkalloc] initWithCoordinate:coords2addressDictionary:nil]];

        toLocation.name =cell.model.title;

        NSArray *items = [NSArrayarrayWithObjects:mylocation, toLocation,nil];

        NSDictionary *options =@{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsMapTypeKey: [NSNumbernumberWithInteger:MKMapTypeStandard],MKLaunchOptionsShowsTrafficKey:@YES};

        //打开苹果自身地图应用,并呈现特定的item

        [MKMapItemopenMapsWithItems:items launchOptions:options];


      //百度地图

        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {//判断是否安装了百度地图APP
             NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/marker?location=%f,%f&title=%@&content=%@&src=webapp.marker.yourCompanyName.yourAppName",cell.model.coordinate.latitude,cell.model.coordinate.longitude,cell.stroeNameLab.text,cell.stroeAddressLab.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

        }else{}


     //高德地图

        NSString *urlString = [[NSStringstringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",cell.model.title,@"baidumap",cell.model.coordinate.latitude, cell.model.coordinate.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:urlString]];

    ----------------------

    //跳到地图导航

     -  ( void ) btnPermanentAddressClick

     { 

        NSString    * appName   =  @  "自己的app名字"   ; 

          NSString    * urlScheme   =  @  "自己的urlScheme"   ; 

          UIAlertController    *  alert   =     [   UIAlertController  alertControllerWithTitle  : @  "选择地图"  message  :   nil  preferredStyle  :   UIAlertControllerStyleActionSheet   ]   ; 

       

       UIAlertAction    *  appleMap   =    [   UIAlertAction  actionWithTitle  : @  "苹果地图"  style  :   UIAlertActionStyleDefault  handler  :   ^   (   UIAlertAction    *   action  )    { 

              if    (   [   [   UIApplication  sharedApplication  ] canOpenURL  :   [   NSURL    URLWithString   : @  "baidumap://"   ]   ]   ) 

              { 

                  //地理编码器 

                  CLGeocoder    * geocoder   =    [   [   CLGeocoder  alloc  ]    init   ]   ; 

                  [ geocoder geocodeAddressString  : @  "上海嘉定区伊宁路2000号"  completionHandler  :   ^   (   NSArray   &lt;   CLPlacemark    *   &gt;    *  _Nullable placemarks  ,    NSError    *  _Nullable error  )    { 

                      for    (   CLPlacemark    * placemark   in  placemarks  )    { 

                          //坐标(经纬度) 

                          CLLocationCoordinate2D  coordinate   =  placemark  . location  . coordinate  ; 

                          MKMapItem    * currentLocation   =    [   MKMapItem  mapItemForCurrentLocation  ]   ; 

                          MKMapItem    * toLocation   =    [   [   MKMapItem  alloc  ]  initWithPlacemark  :   [   [   MKPlacemark  alloc  ]  initWithCoordinate  : coordinate addressDictionary  :   nil   ]   ]   ; 

                          [   MKMapItem  openMapsWithItems  : @  [ currentLocation  ,  toLocation  ] 

                                       launchOptions  : @  {   MKLaunchOptionsDirectionsModeKey   :    MKLaunchOptionsDirectionsModeDriving   , 

                                                         MKLaunchOptionsShowsTrafficKey   :    [   NSNumber  numberWithBool  :   YES   ]   }   ]   ; 

                      } 

                  }   ]   ; 

                

              }   else   { 

                  [   NXPopAlertViewLabel  showMessage  : @  "您的手机没有安装苹果地图"   ]   ; 

              } 

          }   ]   ; 

        

          UIAlertAction    *  baiduMap   =    [   UIAlertAction  actionWithTitle  : @  "百度地图"  style  :   UIAlertActionStyleDefault  handler  :   ^   (   UIAlertAction    *   action  )    { 

              if    (   [   [   UIApplication  sharedApplication  ] canOpenURL  :   [   NSURL    URLWithString   : @  "baidumap://"   ]   ]   ) 

              { 

                  //地理编码器 

                  CLGeocoder    * geocoder   =    [   [   CLGeocoder  alloc  ]    init   ]   ; 

                  [ geocoder geocodeAddressString  : @  "上海嘉定区伊宁路2000号"  completionHandler  :   ^   (   NSArray   &lt;   CLPlacemark    *   &gt;    *  _Nullable placemarks  ,    NSError    *  _Nullable error  )    { 

                      for    (   CLPlacemark    * placemark   in  placemarks  )    { 

                          //坐标(经纬度) 

                          CLLocationCoordinate2D  coordinate   =  placemark  . location  . coordinate  ; 

                          NSString    * urlString   =    [   [   NSString  stringWithFormat  : @  "baidumap://map/direction?origin={{我的位置}}&amp;destination=latlng:%f,%f|name=目的地&amp;mode=driving&amp;coord_type=gcj02"   , coordinate  . latitude  ,  coordinate  . longitude  ]  stringByAddingPercentEncodingWithAllowedCharacters  :   [   NSCharacterSet    URLQueryAllowedCharacterSet   ]   ]   ; 

                          [   [   UIApplication  sharedApplication  ]  openURL  :   [   NSURL    URLWithString   : urlString  ]  options  : @  {   }  completionHandler  :   nil   ]   ; 

                      } 

                  }   ]   ; 

              }   else   { 

                  [   NXPopAlertViewLabel  showMessage  : @  "您的手机没有安装百度地图"   ]   ; 

              } 

          }   ]   ; 

          UIAlertAction    *  gaodeMap   =    [   UIAlertAction  actionWithTitle  : @  "高德地图"  style  :   UIAlertActionStyleDefault  handler  :   ^   (   UIAlertAction    *   action  )    { 

            

              if    (   [   [   UIApplication  sharedApplication  ] canOpenURL  :   [   NSURL    URLWithString   : @  "iosamap://"   ]   ]   ) 

              { 

                  //地理编码器 

                  CLGeocoder    * geocoder   =    [   [   CLGeocoder  alloc  ]    init   ]   ; 

                  [ geocoder geocodeAddressString  : @  "上海嘉定区伊宁路2000号"  completionHandler  :   ^   (   NSArray   &lt;   CLPlacemark    *   &gt;    *  _Nullable placemarks  ,    NSError    *  _Nullable error  )    { 

                      for    (   CLPlacemark    * placemark   in  placemarks  )    { 

                          //坐标(经纬度) 

                          CLLocationCoordinate2D  coordinate   =  placemark  . location  . coordinate  ; 

                          NSString    * urlString   =    [   [   NSString  stringWithFormat  : @  "iosamap://navi?sourceApplication=%@&amp;backScheme=%@&amp;lat=%f&amp;lon=%f&amp;dev=0&amp;style=2"   , appName  , urlScheme  , coordinate  . latitude  ,  coordinate  . longitude  ]  stringByAddingPercentEncodingWithAllowedCharacters  :   [   NSCharacterSet    URLQueryAllowedCharacterSet   ]   ]   ; 

                          [   [   UIApplication  sharedApplication  ]  openURL  :   [   NSURL    URLWithString   : urlString  ]  options  : @  {   }  completionHandler  :   nil   ]   ; 

                      } 

                  }   ]   ; 

              }   else   { 

                  [   NXPopAlertViewLabel  showMessage  : @  "您的手机没有安装高德地图"   ]   ; 

              } 

          }   ]   ; 

          UIAlertAction    *  gugeMap   =    [   UIAlertAction  actionWithTitle  : @  "谷歌地图"  style  :   UIAlertActionStyleDefault  handler  :   ^   (   UIAlertAction    *   action  )    { 

            

              if    (   [   [   UIApplication  sharedApplication  ] canOpenURL  :   [   NSURL    URLWithString   : @  "comgooglemaps://"   ]   ]   ) 

              { 

                  //地理编码器 

                  CLGeocoder    * geocoder   =    [   [   CLGeocoder  alloc  ]    init   ]   ; 

                  [ geocoder geocodeAddressString  : @  "上海嘉定区伊宁路2000号"  completionHandler  :   ^   (   NSArray   &lt;   CLPlacemark    *   &gt;    *  _Nullable placemarks  ,    NSError    *  _Nullable error  )    { 

                      for    (   CLPlacemark    * placemark   in  placemarks  )    { 

                          //坐标(经纬度) 

                          CLLocationCoordinate2D  coordinate   =  placemark  . location  . coordinate  ; 

                          NSString    * urlString   =    [   [   NSString  stringWithFormat  : @  "comgooglemaps://?x-source=%@&amp;x-success=%@&amp;saddr=&amp;daddr=%f,%f&amp;directionsmode=driving"   , appName  , urlScheme  , coordinate  . latitude  ,  coordinate  . longitude  ]  stringByAddingPercentEncodingWithAllowedCharacters  :   [   NSCharacterSet    URLQueryAllowedCharacterSet   ]   ]   ; 

                          [   [   UIApplication  sharedApplication  ]  openURL  :   [   NSURL    URLWithString   : urlString  ]  options  : @  {   }  completionHandler  :   nil   ]   ; 

                      } 

                  }   ]   ; 

              }   else   { 

                  [   NXPopAlertViewLabel  showMessage  : @  "您的手机没有安装谷歌地图"   ]   ; 

              } 

          }   ]   ; 

          UIAlertAction    *  cancel   =    [   UIAlertAction  actionWithTitle  : @  "取消"  style  :   UIAlertActionStyleDefault  handler  :   ^   (   UIAlertAction    *   action  )    { 

            

          }   ]   ; 

          [ alert addAction  : appleMap  ]   ; 

          [ alert addAction  : baiduMap  ]   ; 

          [ alert addAction  : gaodeMap  ]   ; 

          [ alert addAction  : gugeMap  ]   ; 

          [ alert addAction  : cancel  ]   ; 

          [   self  presentViewController  : alert animated  :   YES  completion  :   ^   { 

            

          }   ]   ; 

      } 

    参考链接

    https://www.cnblogs.com/huahua0809/p/5235670.html

    https://blog.csdn.net/autom_lishun/article/details/54092420

    https://www.jianshu.com/p/b7fa7253bc1b

    https://www.jianshu.com/p/ee83aed42d6d

    https://www.jianshu.com/p/27dbe24e8771

    相关开放平台入口

    百度开放平台  http://lbsyun.baidu.com

    百度地图 ios  快速入口  http://lbsyun.baidu.com/index.php?title=iossdk/guide/navigation/allocation

    https://lbsyun.baidu.com/index.php?title=uri/api/ios

    高德地图 ios  快速入口  https://lbs.amap.com/api/amap-mobile/guide/ios/navi

  • 相关阅读:
    Flutter 容器(4)
    Linux 安装maven环境
    ajaxStart、ajaxStop使用注意事项
    jquery里用正则来验证密码,必须包含大小写字母,数字及特殊符号,或最少包含两种
    Nginx 相关介绍(Nginx是什么?能干嘛?配有图片示例)
    常见web攻击总结
    用php的chr和ord函数实现字符串和ASCII码互转
    消息队列设计精要
    mysql索引总结----mysql 索引类型以及创建
    MySQL事务隔离级别详解
  • 原文地址:https://www.cnblogs.com/isItOk/p/5875152.html
Copyright © 2011-2022 走看看