坐标转换 搜狗与实际经纬度工具类

本文深入探讨了编程技术领域的关键概念与实践,包括数据结构、算法、机器学习、数据库理论等,旨在为开发者提供全面的技术洞察与实践指导。从基础的数据结构到复杂的AI应用,本文涵盖了广泛的技术领域,旨在帮助读者提升编程技能,解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



public class MapPoint {
    private double lon;
    private double lat;
    public MapPoint() {
    }

    public void setLon(double lon) {
        this.lon = lon;
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    public double getLon() {
        return lon;
    }

    public double getLat() {
        return lat;
    }
}



public final class a
{

    public a()
    {
        a = 0.0D;
        b = 0.0D;
    }

    public final String toString()
    {
        return "[LatLon]" + b + "," + a;
    }

    public double a;
    public double b;
}

public final class b {

    public b() {
    }

    private static double a(double d1) {
        boolean flag = false;
        if (d1 < 0.0D) {
            d1 = -d1;
            flag = true;
        }
        long l = (long) (d1 / 6.2831853071795862D);
        double d2;
        if ((d2 = d1 - (double) l * 6.2831853071795862D) > 3.1415926535897931D) {
            d2 -= 3.1415926535897931D;
            flag = !flag;
        }
        double d3 = d1 = d2;
        double d4 = d1;
        d2 *= d2;
        d4 *= d2;
        d3 -= d4 * 0.16666666666666699D;
        d4 *= d2;
        d3 += d4 * 0.0083333333333333297D;
        d4 *= d2;
        d3 -= d4 * 0.00019841269841269801D;
        d4 *= d2;
        d3 += d4 * 2.7557319223985901E-006D;
        d4 *= d2;
        d3 -= d4 * 2.50521083854417E-008D;
        if (flag)
            d3 = -d3;
        return d3;
    }

    private static double a1(double d1, double d2) {
        double d3;
        return d3 = (d3 =
                     (d3 = (d3 = 300D + 1.0D * d1 + 2D * d2 +
                            0.10000000000000001D * d1 * d1 +
                            0.10000000000000001D * d1 * d2 +
                            0.10000000000000001D * Math.sqrt(Math.sqrt(d1 * d1))) +
                      (20D * a(18.849555921538759D * d1) +
                       20D * a(6.2831853071795862D * d1)) *
                      0.66669999999999996D) +
                     (20D * a(3.1415926535897931D * d1) +
                      40D * a((3.1415926535897931D * d1) / 3D)) *
                     0.66669999999999996D) +
                                 (150D * a((3.1415926535897931D * d1) / 12D) +
                                  300D * a((3.1415926535897931D * d1) / 30D)) *
                                 0.66669999999999996D;
    }

    private static double b(double d1, double d2) {
        double d3;
        return d3 = (d3 =
                     (d3 = (d3 = -100D + 2D * d1 + 3D * d2 +
                            0.20000000000000001D * d2 * d2 +
                            0.10000000000000001D * d1 * d2 +
                            0.20000000000000001D * Math.sqrt(Math.sqrt(d1 * d1))) +
                      (20D * a(18.849555921538759D * d1) +
                       20D * a(6.2831853071795862D * d1)) *
                      0.66669999999999996D) +
                     (20D * a(3.1415926535897931D * d2) +
                      40D * a((3.1415926535897931D * d2) / 3D)) *
                     0.66669999999999996D) +
                                 (160D * a((3.1415926535897931D * d2) / 12D) +
                                  320D * a((3.1415926535897931D * d2) / 30D)) *
                                 0.66669999999999996D;
    }

    private static double c(double d1, double d2) {
        double d3 = a((d1 * 3.1415926535897931D) / 180D);
        double d4 = Math.sqrt(1.0D - 0.0066934200000000003D * d3 * d3);
        return d4 = (d2 * 180D) /
                    ((6378245D / d4) *
                     Math.cos((d1 * 3.1415926535897931D) / 180D) *
                     3.1415926535897931D);
    }

    private static double d(double d1, double d2) {
        double d3 = a((d1 * 3.1415926535897931D) / 180D);
        double d4 = 1.0D - 0.0066934200000000003D * d3 * d3;
        double d5 = 6335552.7273521004D / (d4 * Math.sqrt(d4));
        return (d2 * 180D) / (d5 * 3.1415926535897931D);
    }

    public static a a(double d1, double d2) {
        a a1 = new a();
        double d3 = a1(d1 - 105D, d2 - 35D);
        double d4 = b(d1 - 105D, d2 - 35D);
        a1.b = d1 + c(d2, d3);
        a1.a = d2 + d(d2, d4);
        return a1;
    }
}




public class MapUtil {

    private static final String mapbarKey =
            "MAPBAR_NAVINFO_2006_XMLAPI_WTWROIEWMFXAPKHDLW";

    private static final double ARC_To_RAD = (Math.PI / 180.0);
    private static final double RAD_TO_ARC = (180.0 / Math.PI);

    public static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    public static double GetDistance(double lng1, double lat1, double lng2,
                                     double lat2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1 - 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 * 10000)) / 10000.0;
        return s;
    }


    public MapUtil() {
    }

    public static double[] decode(String xy) {
        return MapCodec.decode(xy, mapbarKey);
    }

    public static String encode(double x, double y) {
        return MapCodec.encode(y, x, mapbarKey);
    }

    public static long[] diff(long semidiameter, double latitude) {
        double lat_diff = RAD_TO_ARC * (semidiameter /
                                        (EARTH_RADIUS));
        long long_lat_diff = (long) (lat_diff * 100000000.0);
        double lot_diff = RAD_TO_ARC * (semidiameter / (EARTH_RADIUS *
                Math.sin(latitude * ARC_To_RAD)));
        long long_lot_diff = (long) (lot_diff * 10000000.0);
        return new long[] {long_lot_diff, long_lat_diff};
    }

    public static void Map84To02(Point a,Point b){
        double[] d = Map84To02(new double[] {a.x, a.y});
        b.x=d[0];
        b.y=d[1];
    }

    public static double[] Map84To02(double x,double y){
        return Map84To02(new double[]{x,y});
    }

    public static double[] Map84To02(double ad[]) {
        double ad1[] = new double[2];
        double d = 0.0D;
        double d1 = 0.0D;

        a a1 = b.a(ad[1], ad[0]);
        ad1[1] = a1.b + d;
        ad1[0] = a1.a + d1;

        return ad1;
    }


    public static class Point {
        public double x, y;
        public Point() {}

        public Point(double x, double y) {
            this.x = x;
            this.y = y;
        }
    }


    private static double EARTHRADIUS = 6370996.81;
    private static double[] MCBAND = {12890594.86, 8362377.87, 5591021,
                                     3481989.83, 1678043.12,
                                     0};
    private static double[] LLBAND = {75, 60, 45, 30, 15, 0};
    private static double[][] MC2LL = { {1.410526172116255e-008,
                                      8.983055096488720e-006,
                                      -1.99398338163310, 2.009824383106796e+002,
                                      -1.872403703815547e+002,
                                      91.60875166698430,
                                      -23.38765649603339, 2.57121317296198,
                                      -0.03801003308653,
                                      1.733798120000000e+007},
                                      { -7.435856389565537e-009,
                                      8.983055097726239e-006, -0.78625201886289,
                                      96.32687599759846, -1.85204757529826,
                                      -59.36935905485877,
                                      47.40033549296737, -16.50741931063887,
                                      2.28786674699375,
                                      1.026014486000000e+007},
                                      { -3.030883460898826e-008,
                                      8.983055099835780e-006, 0.30071316287616,
                                      59.74293618442277, 7.35798407487100,
                                      -25.38371002664745,
                                      13.45380521110908, -3.29883767235584,
                                      0.32710905363475,
                                      6.856817370000000e+006},
                                      { -1.981981304930552e-008,
                                      8.983055099779535e-006, 0.03278182852591,
                                      40.31678527705744, 0.65659298677277,
                                      -4.44255534477492,
                                      0.85341911805263, 0.12923347998204,
                                      -0.04625736007561,
                                      4.482777060000000e+006},
                                      {3.091913710684370e-009,
                                      8.983055096812155e-006, 0.00006995724062,
                                      23.10934304144901, -0.00023663490511,
                                      -0.63218178102420,
                                      -0.00663494467273, 0.03430082397953,
                                      -0.00466043876332,
                                      2.555164400000000e+006},
                                      {2.890871144776878e-009,
                                      8.983055095805407e-006, -0.00000003068298,
                                      7.47137025468032, -0.00000353937994,
                                      -0.02145144861037,
                                      -0.00001234426596, 0.00010322952773,
                                      -0.00000323890364,
                                      8.260885000000000e+005}
    };

    private static double[][] LL2MC = { { -0.00157021024440,
                                      1.113207020616939e+005,
                                      1.704480524535203e+015,
                                      -1.033898737604234e+016,
                                      2.611266785660388e+016,
                                      -3.514966917665370e+016,
                                      2.659570071840392e+016,
                                      -1.072501245418824e+016,
                                      1.800819912950474e+015, 82.50000000000000},
                                      {8.277824516172526e-004,
                                      1.113207020463578e+005,
                                      6.477955746671608e+008,
                                      -4.082003173641316e+009,
                                      1.077490566351142e+010,
                                      -1.517187553151559e+010,
                                      1.205306533862167e+010,
                                      -5.124939663577472e+009,
                                      9.133119359512032e+008, 67.50000000000000},
                                      {0.00337398766765, 1.113207020202162e+005,
                                      4.481351045890365e+006,
                                      -2.339375119931662e+007,
                                      7.968221547186455e+007,
                                      -1.159649932797253e+008,
                                      9.723671115602145e+007,
                                      -4.366194633752821e+007,
                                      8.477230501135234e+006, 52.50000000000000},
                                      {0.00220636496208, 1.113207020209128e+005,
                                      5.175186112841131e+004,
                                      3.796837749470245e+006,
                                      9.920137397791013e+005,
                                      -1.221952217112870e+006,
                                      1.340652697009075e+006,
                                      -6.209436990984312e+005,
                                      1.444169293806241e+005, 37.50000000000000},
                                      { -3.441963504368392e-004,
                                      1.113207020576856e+005,
                                      2.782353980772752e+002,
                                      2.485758690035394e+006,
                                      6.070750963243378e+003,
                                      5.482118345352118e+004,
                                      9.540606633304236e+003,
                                      -2.710553267466450e+003,
                                      1.405483844121726e+003, 22.50000000000000},
                                      { -3.218135878613132e-004,
                                      1.113207020701615e+005,
                                      0.00369383431289, 8.237256402795718e+005,
                                      0.46104986909093, 2.351343141331292e+003,
                                      1.58060784298199, 8.77738589078284,
                                      0.37238884252424,
                                      7.45000000000000}
    };

    private static double f2(double a) {
        return Math.PI * a / 180;
    }

    private static double f3(double x1, double x2, double y1, double y2) {
        return EARTHRADIUS *
                Math.acos((Math.sin(y1) * Math.sin(y2) +
                           Math.cos(y1) * Math.cos(y2) * Math.cos(x2 - x1)));
    }

    public static void map84ToSohu(Point a, Point b) {
        double[] e = null;
        for (int i = 0; i < LLBAND.length; i++) {
            if (a.y > LLBAND[i]) {
                e = LL2MC[i];
                break;
            }
        }
        f1(b, a, e);
    }

    public static double[] map84ToSohu(double x,double y){
        Point a=new Point(x,y);
        Point b=new Point();
        map84ToSohu(a,b);
        return new double[]{b.x,b.y};
    }

    private static double W(double a) {
        return Math.abs(a);
    }

    private static void f1(Point a, Point b, double[] c) {
        a.x = c[0] + c[1] * W(b.x);
        double f = W(b.y) / c[9];
        a.y = c[2] + c[3] * f + c[4] * f * f + c[5] * f * f * f +
              c[6] * f * f * f * f + c[7] * f * f * f * f * f +
              c[8] * f * f * f * f * f * f;
        a.x *= (b.x < 0 ? -1 : 1);
        a.y *= (b.y < 0 ? -1 : 1);
    }


    public static void sohuToMap84(Point a, Point b) {
        double[] e = null;
        for (int i = 0; i < MCBAND.length; i++) {
            if (a.y > MCBAND[i]) {
                e = MC2LL[i];
                break;
            }
        }
        f1(b, a, e);
    }

    public static double[] sohuToMap84(double x, double y) {
        Point a = new Point(x, y);
        Point b = new Point();
        sohuToMap84(a, b);
        return new double[] {b.x, b.y};
    }

    public static double distanceBySohu(Point a, Point b) {
        Point c = new Point(), d = new Point();
        double x1, y1, x2, y2;
        sohuToMap84(a, c);
        x1 = f2(c.x);
        y1 = f2(c.y);
        sohuToMap84(b, d);
        x2 = f2(d.x);
        y2 = f2(d.y);
        return f3(x1, x2, y1, y2);
    }

 
    public static double distanceByMap02(Point a, Point b) {
        return Math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)) *
                100000*8.01/8.98;
    }

    public static double distanceByMap84(Point a, Point b) {
        double x1, x2, y1, y2;
        x1 = f2(a.x);
        y1 = f2(a.y);
        x2 = f2(b.x);
        y2 = f2(b.y);
        return f3(x1, x2, y1, y2);
    }


    public static void main(String[] args) {

       
        Point a = new Point(1.2743974E7,4364180.0);
        Point b=new Point(1.2743974E7+2e3,4364180.0+2e3);
        Point c=new Point();
        Point d=new Point();
        Point e=new Point();
        Point f=new Point();
        System.out.println(MapUtil.distanceBySohu(a,b));
        MapUtil.sohuToMap84(a, c);
        MapUtil.sohuToMap84(b, d);
        System.out.println(MapUtil.distanceByMap84(c,d));
        MapUtil.Map84To02(c, e);
        MapUtil.Map84To02(d, f);
        System.out.println(c);
        System.out.println(MapUtil.distanceByMap02(e,f));
        
        
        
        System.out.println("11111111"+(double)11585750);
        double dLatLon[]=MapUtil.sohuToMap84((double)11585750, (double)3567500);
        System.out.println("[Dec]" + dLatLon[0] + "," + dLatLon[1]);
        
        System.out.println((int)Math.round(dLatLon[0]*100000));
        
        
        
        double dLatLon2[]=MapUtil.map84ToSohu((double)116.30077837882071
, (double)39.877562372719375);
        System.out.println("[Dec2]" + dLatLon2[0]/100000 + "," + dLatLon2[1]/100000);

    }


}



谷歌坐标转换  地址---->经纬度

//import com.vincent.googlegeocoder.bean.GeocoderBean; 

public class GeoCoder {

	/**
	 * 未出现错误,已对地址成功地进行了解析,并返回其地址解析
	 */
	public static int G_GEO_SUCCESS = 200;
	/**
	 * 无法成功解析行车路线请求。例如,如果此请求包含的路标数大于允许的最大最大数,则该请求可能已被拒绝。
	 */
	public static int G_GEO_BAD_REQUEST = 400;
	/**
	 * 无法成功处理地址解析或行车路线请求,但是确切的失败原因未知。
	 */
	public static int G_GEO_SERVER_ERROR = 500;
	/**
	 * HTTP q 参数缺失或没有值。对于地址解析请求,这意味着将空地址指定为输入。对于行车路线请求,这意味着在输入中未指定查询。
	 */
	public static int G_GEO_MISSING_QUERY = 601;
	/**
	 * 找不到指定地址的对应地理位置。这可能是地址比较新,或地址不正确。
	 */
	public static int G_GEO_UNKNOWN_ADDRESS = 602;
	/**
	 * 由于合法性或合同原因,无法返回指定地址的地址解析或指定行车路线查询的路线。
	 */
	public static int G_GEO_UNAVAILABLE_ADDRESS = 603;
	/**
	 * Gdirections 对象无法计算查询中提到的两点之间的行车路线。这通常是因为两点之间无可用路线,或我们没有该地区的路线数据。
	 */
	public static int G_GEO_UNKNOWN_DIRECTIONS = 604;
	/**
	 * 指定的密钥无效或与指定的域不匹配。
	 */
	public static int G_GEO_BAD_KEY = 610;
	/**
	 * 指定的密钥超出了 24
	 * 小时的请求限制或在过短的一段时间内提交了过多的请求。如果您要同时或循环发送多个请求,请在代码中使用计时器或暂停以确保不会过快地发送请求。
	 */
	public static int G_GEO_TOO_MANY_QUERIES = 620;

	public GeoCoder(String country, String province, String road)
			throws IOException {
		this(country + province + road);
	}

	public GeoCoder(String address) throws IOException {
		this.address = address;
		this.mGeocoderBean = fromJson(getJson());
	}

	public GeoCoder(double lng,double lat) throws Exception {
		this.lng=lng;
		this.lat=lat;
		this.mGeocoderBean=fromJson(getJsonByLatLng());
	}
	
	/**
	 * 获取状态码
	 * 
	 * @return
	 */
	public int getCode() {
		return mGeocoderBean.getStatus().getCode();
	}

	/**
	 * 返回你输入搜索的地址
	 * 
	 * @return
	 */
	public String getSearchAddr() {
		return mGeocoderBean.getName();
	}

	/**
	 * 返回获取到的地址数
	 * 
	 * @return
	 */
	public int getCount() {
		return mGeocoderBean.getPlacemark().size();
	}

	/**
	 * 返回查询得到的第一条数据的经纬度,结果是一个double数组, [0]=longitude [1]=latitude [2]=altitude
	 * 如果没有数据,则返回null
	 * 
	 * @return
	 */
	public double[] getFirstLatLng() {
		if (mGeocoderBean.getPlacemark().size() > 0) {
			return mGeocoderBean.getPlacemark().get(0).getPoint()
					.getCoordinates();
		} else {
			return null;
		}
	}

	private GeocoderBean fromJson(Reader reader) {
		Gson gson = new Gson();
		Type t = new TypeToken<GeocoderBean>() {
		}.getType();
		GeocoderBean mGeocoderBean = gson.fromJson(reader, t);
		return mGeocoderBean;
	}

	private Reader getJson() throws IOException {
		String addr = URLEncoder.encode(address, "utf-8");
		URL url = new URL("http://maps.google.com/maps/geo?q=" + addr
				+ "&oe=utf8&output=json&key=abcdefg");
		URLConnection conn = url.openConnection();
		InputStream is = conn.getInputStream();
		
//		String r=HttpUtil.get("http://maps.google.com/maps/geo?q=" + addr
//				+ "&oe=utf8&output=json&key=abcdefg", "", "utf-8");
//		InputStream is=new ByteArrayInputStream(r.getBytes());
		
		InputStreamReader reader = new InputStreamReader(is);
		return reader;
	}
	
	private Reader getJsonByLatLng() throws IOException {
		URL url = new URL("http://ditu.google.cn/maps/geo?q=" + lat+","+lng
				+ "&oe=utf8&output=json&key=abcdefg");
		URLConnection conn = url.openConnection();
		InputStream is = conn.getInputStream();
		
//		String r=HttpUtil.get("http://ditu.google.cn/maps/geo?q=" + lat+","+lng
//				+ "&oe=utf8&output=json&key=abcdefg", "", "utf-8");
//		InputStream is=new ByteArrayInputStream(r.getBytes());
		
		InputStreamReader reader = new InputStreamReader(is);
		return reader;
	}
	
	private String address;
	public GeocoderBean mGeocoderBean;
	private double lng,lat;
	
	public static double[] decode(String address){
		try {
			GeoCoder mGeocoder = new GeoCoder(address);
			double[] d=mGeocoder.mGeocoderBean.getPlacemark().get(0).getPoint().getCoordinates();
			return d;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String encode(double lng,double lat){
		try{
			GeoCoder mGeocoder = new GeoCoder(lng,lat);
			String address=mGeocoder.mGeocoderBean.getPlacemark().get(0).getAddress();
			return address;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		try {
			GeoCoder mGeocoder = new GeoCoder("北京市海淀区");
			System.out.println(mGeocoder.getCount());
			double[] d=mGeocoder.mGeocoderBean.getPlacemark().get(0).getPoint().getCoordinates();
			System.out.println(d[0]);
			System.out.println(d[1]);
						
			double[] ll=GoogleMapUtil.revert(d[0],d[1]);
			
			System.out.println(ll[1]+","+ll[0]);
			
			// System.out.println(mGeocoder.mGeocoderBean.getStatus().getCount());

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


public class MapCodec
{

    public MapCodec()
    {
    }

    public static String encode(double dLat, double dLon, String strKey)
    {
        StringBuffer sb = new StringBuffer();
        int nLat = (int)Math.round(dLat * 100000D);
        int nLon = (int)Math.round(dLon * 100000D);
        int nKey = 0;
        for(int i = 0; i < strKey.length(); i++)
            nKey += 0xff & strKey.charAt(i);

        sb.append(Integer.toHexString((nLon - nLat) + nKey).toUpperCase());
        int iDelimit = sb.length();
        sb.append(Integer.toHexString(nLon + nLat).toUpperCase());
        char cDelimit = '0';
        for(int i = 0; i < sb.length(); i++)
        {
            char cItem = (char)((sb.charAt(i) - 48) + 65);
            if(cDelimit < cItem)
                cDelimit = cItem;
            sb.setCharAt(i, cItem);
        }

        sb.insert(iDelimit, (char)(cDelimit + 1));
        return sb.toString();
    }

    public static double[] decode(String strLine, String strKey)
    {
        double dLatLon[] = new double[2];
        int nKey = 0;
        for(int i = 0; i < strKey.length(); i++)
            nKey += 0xff & strKey.charAt(i);

        StringBuffer sb = new StringBuffer(strLine);
        int iDelimit = 0;
        char cDelimit = '0';
        for(int i = 0; i < sb.length(); i++)
        {
            char cItem = (char)((sb.charAt(i) - 65) + 48);
            if(cDelimit < sb.charAt(i))
            {
                cDelimit = sb.charAt(i);
                iDelimit = i;
            }
            sb.setCharAt(i, cItem);
        }

        int n1 = Integer.parseInt(sb.substring(0, iDelimit), 16);
        int n2 = Integer.parseInt(sb.substring(iDelimit + 1), 16);
        int nLon = ((n1 + n2) - nKey) / 2;
        int nLat = n2 - nLon;
        dLatLon[1] = (double)nLat / 100000D;
        dLatLon[0] = (double)nLon / 100000D;
        return dLatLon;
    }

    public static void main(String args[])
    {
        String strKey = "127.0.0.1";
        String strEnc = encode(39.9219,116.3914, strKey);
        System.out.println("[Enc]" + strEnc);
        double dLatLon[] = decode(strEnc, strKey);
        System.out.println("[Dec]" + dLatLon[0] + "," + dLatLon[1]);
    }
}



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值