火星坐标

来源:互联网 发布:淘宝不要搜血滴子 编辑:程序博客网 时间:2024/04/27 15:48

一、坐标分类

地图坐标大致分为几种:

1、GPS、WGS84,也就是原始坐标体系,这是国际公认的世界标准坐标体系;

2、GCJ-02,又称为“火星坐标”,国家测绘局在02年发布的坐标体系,在国内,至少得使用此坐标体系,比如:google、高德、腾讯地图等;

3、其他特殊坐标体系,一般都是由火星坐标通过偏移算法计算得出的,比如百度使用的是BD-09坐标,搜狗使用的是自己的搜狗坐标。


二、坐标转换

1、使用高德地图sdk转换

[java] view plain copy
  1. public AMapLocation fromGpsToAmap(Location location) {  
  2.         AMapLocation aMapLocation = new AMapLocation(location);  
  3.         CoordinateConverter converter = new CoordinateConverter(mContext);  
  4.         converter.from(CoordinateConverter.CoordType.GPS);  
  5.         try {  
  6.             converter.coord(new DPoint(location.getLatitude(), location.getLongitude()));  
  7.             DPoint desLatLng = converter.convert();  
  8.             aMapLocation.setLatitude(desLatLng.getLatitude());  
  9.             aMapLocation.setLongitude(desLatLng.getLongitude());  
  10.         } catch (Exception e) {  
  11.             e.printStackTrace();  
  12.         }  
  13.         return aMapLocation;  
  14.     }  
但是在我的项目里面,当使用上面方法的高德地图版本的jar包后,编译的时候友盟总是提示我有包冲突,但是经历无数的寻找,都没找出冲突的地方,当我把友盟统计的包引用去掉,编译正常与行了。这里我被友盟坑了,但是必须要保留友盟统计。我只能放弃新的定位包,使用老版本的,也就不能用上面这个方式了。

2、自己转换

通过在网上的搜索,找到一篇文章http://www.eoeandroid.com/forum.php?mod=viewthread&tid=332419,能很好的解决我的问题,也就是我们自己转换坐标,方法如下。

[java] view plain copy
  1. public AMapLocation fromGpsToAmap(Location location) {  
  2.     LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());  
  3.     latLng = CoordinateUtil.transformFromWGSToGCJ(latLng);  
  4.     AMapLocation aMapLocation = new AMapLocation(location);  
  5.     aMapLocation.setLatitude(latLng.latitude);  
  6.     aMapLocation.setLongitude(latLng.longitude);  
  7.   
  8.     return aMapLocation;  
  9. }  

CoordinateUtil.java

[java] view plain copy
  1. public class CoordinateUtil {  
  2.     private static double a = 6378245.0;  
  3.     private static double ee = 0.00669342162296594323;  
  4.   
  5.     /** 
  6.      * 手机GPS坐标转火星坐标 
  7.      * 
  8.      * @param wgLoc 
  9.      * @return 
  10.      */  
  11.     public static LatLng transformFromWGSToGCJ(LatLng wgLoc) {  
  12.   
  13.         //如果在国外,则默认不进行转换  
  14.         if (outOfChina(wgLoc.latitude, wgLoc.longitude)) {  
  15.             return new LatLng(wgLoc.latitude, wgLoc.longitude);  
  16.         }  
  17.         double dLat = transformLat(wgLoc.longitude - 105.0,  
  18.                 wgLoc.latitude - 35.0);  
  19.         double dLon = transformLon(wgLoc.longitude - 105.0,  
  20.                 wgLoc.latitude - 35.0);  
  21.         double radLat = wgLoc.latitude / 180.0 * Math.PI;  
  22.         double magic = Math.sin(radLat);  
  23.         magic = 1 - ee * magic * magic;  
  24.         double sqrtMagic = Math.sqrt(magic);  
  25.         dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * Math.PI);  
  26.         dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * Math.PI);  
  27.   
  28.         return new LatLng(wgLoc.latitude + dLat, wgLoc.longitude + dLon);  
  29.     }  
  30.   
  31.     public static double transformLat(double x, double y) {  
  32.         double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y  
  33.                 + 0.2 * Math.sqrt(x > 0 ? x : -x);  
  34.         ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x  
  35.                 * Math.PI)) * 2.0 / 3.0;  
  36.         ret += (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0  
  37.                 * Math.PI)) * 2.0 / 3.0;  
  38.         ret += (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y  
  39.                 * Math.PI / 30.0)) * 2.0 / 3.0;  
  40.         return ret;  
  41.     }  
  42.   
  43.     public static double transformLon(double x, double y) {  
  44.         double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1  
  45.                 * Math.sqrt(x > 0 ? x : -x);  
  46.         ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x  
  47.                 * Math.PI)) * 2.0 / 3.0;  
  48.         ret += (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0  
  49.                 * Math.PI)) * 2.0 / 3.0;  
  50.         ret += (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x  
  51.                 / 30.0 * Math.PI)) * 2.0 / 3.0;  
  52.         return ret;  
  53.     }  
  54.   
  55.     public static boolean outOfChina(double lat, double lon) {  
  56.         if (lon < 72.004 || lon > 137.8347)  
  57.             return true;  
  58.         if (lat < 0.8293 || lat > 55.8271)  
  59.             return true;  
  60.         return false;  
  61.     }  
  62.   
  63. }  
原创粉丝点击