☆☆☆★☆☆☆

唯有努力才能活成自己想要活成的样子

导航

gps使用的记录

1.通过java来计算两个gps点的距离
public class GoogleMapHelper {

    private static double EARTH_RADIUS = 6378.137;  //地球半径
    //将用角度表示的角转换为近似相等的用弧度表示的角 Math.toRadians
    private static double rad(double d)
    {
        return d * Math.PI / 180.0;
    }

    /**
     * 谷歌地图计算两个坐标点的距离
     * @param lng1  经度1
     * @param lat1  纬度1
     * @param lng2  经度2
     * @param lat2  纬度2
     * @return 距离(千米)
     */
    public static double getDistance(double lng1, double lat1, double lng2, double lat2)
    {
        double radLat1 = Math.toRadians(lat1);
        double radLat2 = Math.toRadians(lat2);
        double a = radLat1 - radLat2;
        double b = Math.toRadians(lng1) - Math.toRadians(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
                Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 1000);
        return s;
    }

    public static void main(String[] args) {
        double dist1 = getDistance(83.678058,46.507023, 83.669613,46.507079);
        System.out.println("两点相距:" + dist1 + "米");  //两点相距:1.0千米

    }
}

 

2.谷歌地图gps点纠偏

package com.hdbimsapi.utils;

import java.util.ArrayList;
import java.util.List;

public class GpsCorrect {
    final static double pi = 3.14159265358979324;
    final static double a = 6378245.0;
    final static double ee = 0.00669342162296594323;
    
    //批量 116.342534,30.232432;116.242324,30.343223
    public static ArrayList<String> getGpsCorrectList(String correct){
        ArrayList<String> gpsList=new ArrayList<String>();
        String [] lnglatStr=correct.split(";");
        for(int i=0;i<lnglatStr.length;i++){
            String [] lnglatTag=lnglatStr[i].split(",");
            gpsList.add(getGpsCorrect(lnglatTag[0], lnglatTag[1]));
        }
        return gpsList;
    }
    
  //ArrayList<gpsPointString>, eg. <116.342534,30.232432;116.242324,30.343223>
    public static List<String> getGpsCorrectList(List<String> rawGpsList)
    {
        List<String> gpsList=new ArrayList<String>();
//        String [] lnglatStr=correct.split(";");
        for(int i=0;i<rawGpsList.size();i++)
        {
            String [] lnglatTag=rawGpsList.get(i).split(",");
            gpsList.add(getGpsCorrect(lnglatTag[0], lnglatTag[1]));
        }
        return gpsList;
    }
    /**
     * 经纬度 116.342534 - 30.232432
     * lonTag 经度
     * latTag 纬度
     */
    public static String getGpsCorrect(String lonTag,String latTag) {
        String lnglat="0,0";
        double wgLat=Double.parseDouble(latTag);
        double wgLon=Double.parseDouble(lonTag);
        if (!outOfChina(wgLat, wgLon)) {
            double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
            double dLon = transformLon(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);
            lnglat=(wgLon + dLon)+","+(wgLat + dLat);
        }
        return lnglat;
    }
    
    public static void transform(double wgLat, double wgLon, double[] latlng) {
        if (outOfChina(wgLat, wgLon)) {
            latlng[0] = wgLat;
            latlng[1] = wgLon;
            return;
        }
        double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
        double dLon = transformLon(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);
        latlng[0] = wgLat + dLat;
        latlng[1] = wgLon + dLon;
    }
    //非国内经纬度
    public static boolean outOfChina(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 transformLat(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 transformLon(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;
    }
    
    public static void main(String[] args) {
        System.out.println(GpsCorrect.getGpsCorrect("116.342534",  "30.232432"));
    }
}

 

posted on 2019-07-02 13:49  Yr-Zhang  阅读(366)  评论(0编辑  收藏  举报