球面上两点之间的距离计算

球面上的两点之间距离计算要先将经纬度坐标转化为墨卡托投影坐标,再用点到直线的距离来进行计算,网上搜的一部分资料整合之后的JAVA代码,不一对,先留着:

资料来源:http://blog.sina.com.cn/s/blog_8ab3b2cf0100xd69.html

     http://blog.csdn.net/ltfgood/article/details/6889509

  1 package com.suncreate.spatialquery.web.utils;
  2 
  3 public class LatitudeLontitudeUtil {  
  4 
  5     // http://blog.charlee.li/location-search/  
  6 
  7     /** 地球半径 */  
  8     private static final double EARTH_RADIUS = 6371393;  
  9 
 10     static double M_PI = Math.PI;
 11 
 12     public LatitudeLontitudeUtil() {  
 13 
 14     }  
 15 
 16     //经纬度转墨卡托
 17     // 经度(lon),纬度(lat)
 18     public static double[] lonLat2Mercator(double lon,double lat)
 19     {
 20         double[] xy = new double[2];
 21         double x = lon *20037508.342789/180;
 22         double y = Math.log(Math.tan((90+lat)*M_PI/360))/(M_PI/180);
 23         y = y *20037508.34789/180;
 24         xy[0] = x;
 25         xy[1] = y;
 26         return xy;
 27     }
 28 
 29     //---------------------------------------------------
 30     // 点到直线的最短距离的判断 点(x0,y0) 到由两点组成的线段(x1,y1) ,( x2,y2 )  
 31     public static double pointToLine(double x1, double y1, double x2, double y2, double x0, double y0) {  
 32         double space = 0;  
 33         double a, b, c;  
 34         a = lineSpace(x1, y1, x2, y2);// 线段的长度  
 35         b = lineSpace(x1, y1, x0, y0);// (x1,y1)到点的距离  
 36         c = lineSpace(x2, y2, x0, y0);// (x2,y2)到点的距离  
 37 
 38         System.out.println("a:"+a+",b:"+b+",c:"+c);
 39 
 40         if (c + b == a) {//点在线段上  
 41             space = 0;  
 42             return space;  
 43         }  
 44         if (a <= 0.000001) {//不是线段,是一个点  
 45             space = b;  
 46             return space;  
 47         }  
 48 
 49         if (c * c >= a * a + b * b) { //组成直角三角形或钝角三角形,(x1,y1)为直角或钝角  
 50             space = b;  
 51             return space;  
 52         }  
 53         if (b * b >= a * a + c * c) {//组成直角三角形或钝角三角形,(x2,y2)为直角或钝角  
 54             space = c;  
 55             return space;  
 56         }  
 57         //组成锐角三角形,则求三角形的高  
 58         double p = (a + b + c) / 2;// 半周长  
 59         double s = Math.sqrt(p * (p - a) * (p - b) * (p - c));// 海伦公式求面积  
 60         space = 2 * s / a;// 返回点到线的距离(利用三角形面积公式求高)  
 61         return space ;  
 62 
 63     }  
 64 
 65     // 计算两点之间的距离  
 66     public static double lineSpace(double x1, double y1, double x2, double y2) {  
 67         double lineLength = 0;  
 68         lineLength = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));  
 69         return lineLength;  
 70     }  
 71     //------------------------------------------------------------ 
 72 
 73 
 74 
 75 
 76     public static void main(String[] args) {  
 77         //测试用例
 78 
 79         //点到直线的距离算法测试
 80 
 81         //算法一
 82         double x1 = 31.821018461732;  
 83         double y1 = 117.18765906006398;  
 84 
 85         double x2 = 31.821018461732;  
 86         double y2 = 117.18980211911884;  
 87 
 88         double x0 = 31.8226736;
 89         double y0 = 117.1888475;
 90 
 91         double[] xytemp = new double[2];
 92 
 93         xytemp = LatitudeLontitudeUtil.lonLat2Mercator(y1, x1);
 94         double x1m = xytemp[1];
 95         double y1m = xytemp[0];
 96 
 97         xytemp = LatitudeLontitudeUtil.lonLat2Mercator(y2, x2);
 98         double x2m = xytemp[1];
 99         double y2m = xytemp[0];
100 
101         xytemp = LatitudeLontitudeUtil.lonLat2Mercator(y0, x0);
102         double x0m = xytemp[1];
103         double y0m = xytemp[0];
104 
105         System.out.println("x1m:"+x1m+",y1m:"+y1m);
106         System.out.println("x2m:"+x2m+",y2m:"+y2m);
107         System.out.println("x0m:"+x0m+",y0m:"+y0m);
108 
109         double d = LatitudeLontitudeUtil.pointToLine(x1m, y1m, x2m, y2m, x0m, y0m);  
110 
111         System.out.println(d);  
112 
113 
114     }  
115 }  
原文地址:https://www.cnblogs.com/chuang8/p/4556143.html