当前位置: 移动技术网 > IT编程>开发语言>Java > 标准坐标系与火星坐标系(高德)百度坐标系之间互转

标准坐标系与火星坐标系(高德)百度坐标系之间互转

2018年11月16日  | 移动技术网IT编程  | 我要评论

计算机培训机构,拍案网,色欲神魔

这里先给大家介绍几个坐标系:

1.wgs84:国际坐标系,为一种大地坐标系,也是目前广泛使用的gps全球卫星定位系统使用的坐标系。
2.gcj02:火星坐标系,是由中国国家测绘局制订的地理信息系统的坐标系统。由wgs84坐标系经加密后的坐标系。
3.bd09:为百度坐标系,在gcj02坐标系基础上再次加密。其中bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托米制坐标

今天我要说的是,我们大部分的定位gps设备以及硬件都是获取的wgs84坐标也就是我们说的标准坐标系

但是我们软件应用层一般使用的高德,百度,腾讯的地图api,如果直接使用标准坐标系定位是会存在偏差的,所以就需要我们进行转换才能使用,这里给大家提供几种方式:

1.硬件设备获取的gps坐标格式是

北纬:2937.1453(29°37.1453′) 东经:10629.7713(106°29.7713′)
转换成度格式:
北纬:29+37.1453/60=29.61908 东经:106+29.7713/60=106.49618

就是需要我们转换后才能使用,在最近一个项目中,硬件设备传到服务器的gps格式是【3028.0979】【10400.4032】但是服务器接收到是【30, 28, 9, 79】【1, 4, 0, 40, 32】这样的格式 有没有细心的朋友发现少了几位,0在byte字节传输中会被移除,所以这里就需要我们进行第一次转换:

public static double bytetodouble(byte[] com) {
        string str = "";
        double dd = 0.00;
        for (int i = 0; i < com.length; i++) {
            string s = (int) com[i] + "";
            if (s.length() != 2 && i > 0) {
                str += "0" + com[i] + "";
            } else {
                str += com[i] + "";
            }
            dd = integer.parseint(str) / 10000.00;
        }
        return dd;
 }

转后的结果为:【3028.0979,10400.4032】这才是硬件设备传给我们的数据

其次我们还需要在做处理:

double lat = 0.0;
double lng = 0.0;
string lats = byteutil.bytetodouble(_lat) + "";
string lngs = byteutil.bytetodouble(_lng) + "";
int index_lat = lats.indexof(".") - 2;
int index_lng = lngs.indexof(".") - 2;
    lat = double.parsedouble(lats.substring(0, index_lat)) + double.parsedouble(lats.substring(index_lat)) / 60;
    lng = double.parsedouble(lngs.substring(0, index_lng)) + double.parsedouble(lngs.substring(index_lng)) / 60;
 

这里转换后出来的结果是:【30.468298333333333,104.00672】当然当目前为止我们所转换处理的都任然是标准坐标系,还不是我们能放到高德之类的api去使用。

最关键的就是转换成火星坐标:

double[] gaodegps = gpsutil.togcj02point(lat, lng, 7);// 进行纠偏
 

这里我给大家提供一个封装的工具类:  

import java.math.bigdecimal;
import java.math.roundingmode;
 
/**
 * gps纠偏算法,适用于google,高德体系的地图
 */
public abstract class gpsutil {
 
    private final static double a = 6378245.0;
    private final static double pi = 3.1415926535897932384626;
    private final static double ee = 0.00669342162296594323;
 
    /**
     * 计算地球上任意两点(经纬度)距离
     * 
     * @param lat1
     *            第一点纬度
     * @param lng1
     *            第一点经度
     * @param lat2
     *            第二点纬度
     * @param lng2
     *            第二点经度
     * @return 返回距离 单位:米
     */
    public static double distance(double lat1, double lng1, double lat2, double lng2) {
        double a, b, r;
        r = 6378137; // 地球半径
        lat1 = lat1 * math.pi / 180.0;
        lat2 = lat2 * math.pi / 180.0;
        a = lat1 - lat2;
        b = (lng1 - lng2) * math.pi / 180.0;
        double d;
        double sa2, sb2;
        sa2 = math.sin(a / 2.0);
        sb2 = math.sin(b / 2.0);
        d = 2 * r * math.asin(math.sqrt(sa2 * sa2 + math.cos(lat1) * math.cos(lat2) * sb2 * sb2));
        return d;
    }
 
    /**
     * description: wgs-84 to gcj-02 <br>
     * 
     * @author dsn
     * @date 2017年10月24日 下午2:09:27
     * @param latitude
     *            纬度
     * @param longitude
     *            经度
     * @return [纬度,经度]
     * @version 1.0
     */
    public static double[] togcj02point(double latitude, double longitude) {
        double[] dev = caldev(latitude, longitude);
        double retlat = latitude + dev[0];
        double retlon = longitude + dev[1];
        return new double[] { retlat, retlon };
    }
 
    /**
     * description: wgs-84 to gcj-02 <br>
     * 
     * @author dsn
     * @date 2017年10月24日 下午2:09:27
     * @param latitude
     *            纬度
     * @param longitude
     *            经度
     * @param scale
     *            经纬度保留小数位数
     * @return [纬度,经度]
     * @version 1.0
     */
    public static double[] togcj02point(double latitude, double longitude, int scale) {
        double[] dev = caldev(latitude, longitude);
        double retlat = latitude + dev[0];
        double retlon = longitude + dev[1];
        return new double[] { new bigdecimal(retlat).setscale(scale, roundingmode.down).doublevalue(),
                new bigdecimal(retlon).setscale(scale, roundingmode.down).doublevalue() };
    }
 
    /**
     * description:gcj-02 to wgs-84 <br>
     * 
     * @author dsn
     * @date 2017年10月24日 下午2:09:54
     * @param latitude
     *            纬度
     * @param longitude
     *            经度
     * @return [纬度,经度]
     * @version 1.0
     */
    public static double[] towgs84point(double latitude, double longitude) {
        double[] dev = caldev(latitude, longitude);
        double retlat = latitude - dev[0];
        double retlon = longitude - dev[1];
        dev = caldev(retlat, retlon);
        retlat = latitude - dev[0];
        retlon = longitude - dev[1];
        return new double[] { retlat, retlon };
    }
 
    private static double[] caldev(double wglat, double wglon) {
        if (isoutofchina(wglat, wglon)) {
            return new double[] { 0, 0 };
        }
        double dlat = callat(wglon - 105.0, wglat - 35.0);
        double dlon = callon(wglon - 105.0, wglat - 35.0);
        double radlat = wglat / 180.0 * pi;
        double magic = math.sin(radlat);
        magic = 1 - ee * magic * magic;
        double sqrtmagic = math.sqrt(magic);
        dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
        dlon = (dlon * 180.0) / (a / sqrtmagic * math.cos(radlat) * pi);
        return new double[] { dlat, dlon };
    }
 
    private static boolean isoutofchina(double lat, double lon) {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    }
 
    private static double callat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * math.sqrt(math.abs(x));
        ret += (20.0 * math.sin(6.0 * x * pi) + 20.0 * math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * math.sin(y * pi) + 40.0 * math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * math.sin(y / 12.0 * pi) + 320 * math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }
 
    private static double callon(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * math.sqrt(math.abs(x));
        ret += (20.0 * math.sin(6.0 * x * pi) + 20.0 * math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * math.sin(x * pi) + 40.0 * math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * math.sin(x / 12.0 * pi) + 300.0 * math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
        return ret;
    }
 
}

最终转为后的gps坐标为:【30.4656684,104.0090429】这里我是以标准坐标系转为火星坐标系的,其他的转法工具类里面有提供。

还有一种方式,是高德api提供的其他坐标系转高德坐标系

var gps = [116.3, 39.9];
amap.convertfrom(gps, 'gps', function (status, result) {
  if (result.info === 'ok') {
    var lnglats = result.locations; // array.<lnglat>
  }
});

如有需要可以加我q群【308742428】大家一起讨论技术。

后面会不定时为大家更新文章,敬请期待。

喜欢的朋友可以关注下。

  

  

  

  

  

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网