2点(緯度座標)の距離を解く
6137 ワード
1 package edu.cloud.editmap.utils;
2
3 import org.junit.Test;
4 /**
5 *
6 * @author lingfeng
7 */
8 public class LatLon2Distance {
9
10 //
11 private static double EARTH_RADIUS = 6378.137;
12 /**
13 *
14 * @param d
15 * @return
16 */
17 private static double rad(double d)
18 {
19 return d * Math.PI / 180.0;
20 }
21 /**
22 *
23 * @param lat1
24 * @param lng1
25 * @param lat2
26 * @param lng2
27 * @return
28 */
29 public static double getDistance(double lat1, double lng1, double lat2, double lng2)
30 {
31 double radLat1 = rad(lat1);
32 double radLat2 = rad(lat2);
33 double a = radLat1 - radLat2;
34 double b = rad(lng1) - rad(lng2);
35
36 double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
37 Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
38 s = s * EARTH_RADIUS;
39 s = Math.round(s * 10000) / 10000;
40 return s;
41 }
42
43 @Test
44 public void test(){
45 //System.out.println(getDistance(25.2803229,110.3083241,25.289289679357516,110.30976545104478));
46 }
47 }