WGS84、GCJ02、BD911坐标相互转换

1、WGS84和BD911坐标相互转换使用百度地图API,代码如下:

    /**
     * WGS84坐标转换BD911坐标
     *  利用百度地图Api转换
     * @param longitude
     * @param latitude
     * @return
     */
    public static LatLng WGS84ToBd911(double longitude, double latitude){
        LatLng sourceLatLng=new LatLng(latitude,longitude);
        CoordinateConverter converter  = new CoordinateConverter();
        converter.from(CoordType.GPS);
        // sourceLatLng待转换坐标  
        converter.coord(sourceLatLng);  
        LatLng desLatLng = converter.convert();
        return desLatLng;
    }

2、JCG02转WGS84坐标系

    /**
     * gcj02坐标转换为WGS84坐标
     * @param gcj02Lon
     * @param gcj02Lat
     * @return
     */
    public static YNLocation gcj02ToWGS84(double gcj02Lon,double gcj02Lat){
        CoordsUtils coor = new CoordsUtils();
        YNLocation location = coor.gcj_decrypt(gcj02Lat,gcj02Lon);
        return location;
    }

JCG02坐标转换为GPS坐标工具类

/**
 * JCG02坐标转换为GPS坐标工具类
 *  参考网上资料
 * @date 2016年9月29日 下午2:59:25
 * @version V1.0
 */
public class CoordsUtils {

    private double PI = 3.14159265358979324;
    private double X_PI = 3.14159265358979324 * 3000.0 / 180.0;

    public YNLocation gcj_decrypt(double lat, double lon) {
        YNLocation d = delta(lat, lon);
        double latitude = lat-d.getLatitude();
        double longitude = lon-d.getLongitude();
        YNLocation location = new YNLocation();
        location.setLongitude(longitude);
        location.setLatitude(latitude);
        return location;
    }

    public YNLocation delta(double lat, double lon){
        double a = 6378245.0; //  a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
        double ee = 0.00669342162296594323; //  ee: 椭球的偏心率。
        double dLat = this.functionLat(lon - 105.0, lat - 35.0);
        double dLon = this.functionLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * this.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) * this.PI);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * this.PI);
        YNLocation loc = new YNLocation();
        loc.setLongitude(dLon);
        loc.setLatitude(dLat);
        return loc;
    }

    private double functionLat (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 * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin(y / 3.0 * this.PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * this.PI) + 320 * Math.sin(y * this.PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    private double functionLon (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 * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin(x / 3.0 * this.PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * this.PI) + 300.0 * Math.sin(x / 30.0 * this.PI)) * 2.0 / 3.0;
        return ret;
    }

}

坐标实体类

/**
 * 位置信息
 */
public class YNLocation {

    /**
     * 经度
     */
    private double longitude;

    /**
     * 纬度
     */
    private double latitude;

    /**
     * 海拔
     */
    private double altitude;

    /**
     * 速度
     */
    private double speed;

    /**
     * 方向
     */
    private double direction;


    public YNLocation() {
        super();
    }
    public YNLocation(double longitude, double latitude) {
        super();
        this.longitude = longitude;
        this.latitude = latitude;
    }
    public double getLongitude() {
        return longitude;
    }
    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }
    public double getLatitude() {
        return latitude;
    }
    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }
    public double getAltitude() {
        return altitude;
    }
    public void setAltitude(double altitude) {
        this.altitude = altitude;
    }
    public double getSpeed() {
        return speed;
    }
    public void setSpeed(double speed) {
        this.speed = speed;
    }
    public double getDirection() {
        return direction;
    }
    public void setDirection(double direction) {
        this.direction = direction;
    }
}

你可能感兴趣的:(Android开发)