求出在某個經緯度方圓多少公裏返回的最大最小經緯度
阿新 • • 發佈:2018-09-17
param result 賦值 地球 gps 返回 edi pack elong
java代碼,計算在地圖上某個點,以這個點為中心,覆蓋若幹公裏範圍的最大和最小經緯度
package test; public class MainClass { public final static class GpsUtil { double pi; double distance; //地球平均半徑6371km public static final double EARTH_RADIUS = 6371000; public GpsUtil() {this.pi = Math.PI;//3.141592654; //測試好久,發現3535出來的是附近五公裏- -! distance = 3535; } /** * * @param lat 維度 * @param lon 經度 * @param distance 多少米範圍 * @return */ public double[] compute(double lat, doublelon, double distance) { lat = lat * pi / 180; lon = lon * pi / 180; //先換算成弧度 double rad_dist = distance / EARTH_RADIUS; //計算X公裏在地球圓周上的弧度 double lat_min = lat - rad_dist; double lat_max = lat + rad_dist; //計算緯度範圍 doublelon_min; double lon_max; //因為緯度在-90度到90度之間,如果超過這個範圍,按情況進行賦值 if (lat_min > -pi / 2 && lat_max < pi / 2) { //開始計算經度範圍 double lon_t = Math.asin(Math.sin(rad_dist) / Math.cos(lat)); lon_min = lon - lon_t; //同理,經度的範圍在-180度到180度之間 if (lon_min < -pi) lon_min += 2 * pi; lon_max = lon + lon_t; if (lon_max > pi) lon_max -= 2 * pi; } else { lat_min = Math.max(lat_min, -pi / 2); lat_max = Math.min(lat_max, pi / 2); lon_min = -pi; lon_max = pi; } //最後置換成角度進行輸出 lat_min = lat_min * 180 / pi; lat_max = lat_max * 180 / pi; lon_min = lon_min * 180 / pi; lon_max = lon_max * 180 / pi; double result[] = { lat_min, lat_max, lon_min, lon_max }; return result; } /** * 轉化為弧度(rad) */ private double rad(double d) { return d * Math.PI / 180.0; // return d; } /** * 基於余弦定理求兩經緯度距離 * * @param lon1 * 第一點的精度 * @param lat1 * 第一點的緯度 * @param lon2 * 第二點的精度 * @param lat2 * 第二點的緯度 * @return 返回的距離,單位m */ public double LantitudeLongitudeDist(double lat1, double lat2, double lon1, double lon2) { double radLat1 = rad(lat1); double radLat2 = rad(lat2); double radLon1 = rad(lon1); double radLon2 = rad(lon2); if (radLat1 < 0) radLat1 = Math.PI / 2 + Math.abs(radLat1);// south if (radLat1 > 0) radLat1 = Math.PI / 2 - Math.abs(radLat1);// north if (radLon1 < 0) radLon1 = Math.PI * 2 - Math.abs(radLon1);// west if (radLat2 < 0) radLat2 = Math.PI / 2 + Math.abs(radLat2);// south if (radLat2 > 0) radLat2 = Math.PI / 2 - Math.abs(radLat2);// north if (radLon2 < 0) radLon2 = Math.PI * 2 - Math.abs(radLon2);// west double x1 = EARTH_RADIUS * Math.cos(radLon1) * Math.sin(radLat1); double y1 = EARTH_RADIUS * Math.sin(radLon1) * Math.sin(radLat1); double z1 = EARTH_RADIUS * Math.cos(radLat1); double x2 = EARTH_RADIUS * Math.cos(radLon2) * Math.sin(radLat2); double y2 = EARTH_RADIUS * Math.sin(radLon2) * Math.sin(radLat2); double z2 = EARTH_RADIUS * Math.cos(radLat2); double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2)); //余弦定理求夾角 double theta = Math.acos((EARTH_RADIUS * EARTH_RADIUS + EARTH_RADIUS * EARTH_RADIUS - d * d) / (2 * EARTH_RADIUS * EARTH_RADIUS)); double dist = theta * EARTH_RADIUS; return dist; } } public static void main(String[] args) { GpsUtil gps = new GpsUtil(); double[] a = gps.compute(30.01254012452224, 121.01244544525456456478797, 3535); for (double d : a) { System.out.println(d); } double dis = gps.LantitudeLongitudeDist(a[0], a[1], a[2], a[3]); System.out.println("兩點之間距離:" + dis + "米"); } }
求出在某個經緯度方圓多少公裏返回的最大最小經緯度