package com.yuncai.core.common.utils; import java.util.HashMap; import java.util.Map; /** * 计算两个坐标的距离 * @author audaque * */ public class DistanceUtils { private static final double EARTH_RADIUS = 6378137;//赤道半径(单位m) private DistanceUtils(){ } /** * 转化为弧度(rad) * */ private static double rad(double d) { return d * Math.PI / 180.0; } /** * 基于googleMap中的算法得到两经纬度之间的距离,计算精度与谷歌地图的距离精度差不多,相差范围在0.2米以下 * @param lng1 第一点的精度 * @param lat1 第一点的纬度 * @param lng2 第二点的精度 * @param lat2 第二点的纬度 * @return 返回的距离(凑整),单位m * */ public static int 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) - rad(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; return (int) Math.ceil(s); } /* * * * @param raidus 单位米 * return minLng,minLat, maxLng,maxLat */ public static double[] getAround(double lat, double lon, int raidus) { Double latitude = lat; Double longitude = lon; Double degree = (24901 * 1609) / 360.0; double raidusMile = raidus; Double dpmLat = 1 / degree; Double radiusLat = dpmLat * raidusMile; Double minLat = latitude - radiusLat; Double maxLat = latitude + radiusLat; Double mpdLng = degree * Math.cos(latitude * (Math.PI / 180)); Double dpmLng = 1 / mpdLng; Double radiusLng = dpmLng * raidusMile; Double minLng = longitude - radiusLng; Double maxLng = longitude + radiusLng; return new double[]{minLng,minLat, maxLng,maxLat}; } /** * 计算经纬度点对应正方形4个点的坐标 */ public static class DistanceProperty{ public final static String LEFT_TOP_POINT = "leftTopPoint" ; public final static String LEFT_BOTTOM_POINT = "leftBottomPoint"; public final static String RIGHT_TOP_POINT = "rightTopPoint"; public final static String RIGHT_BOTTOM_POINT = "rightBottomPoint"; } public static Map<String, double[]> returnLLSquarePoint(double longitude, double latitude, double distance) { Map<String, double[]> squareMap = new HashMap<String, double[]>(); // 计算经度弧度,从弧度转换为角度 double dLongitude = 2 * (Math.asin(Math.sin(distance / (2 * EARTH_RADIUS)) / Math.cos(Math.toRadians(latitude)))); dLongitude = Math.toDegrees(dLongitude); // 计算纬度角度 double dLatitude = distance / EARTH_RADIUS; dLatitude = Math.toDegrees(dLatitude); // 正方形 double[] leftTopPoint = { longitude - dLongitude,latitude + dLatitude }; double[] rightTopPoint = {longitude + dLongitude,latitude + dLatitude }; double[] leftBottomPoint = {longitude - dLongitude,latitude - dLatitude }; double[] rightBottomPoint = { longitude + dLongitude ,latitude - dLatitude}; squareMap.put(DistanceProperty.LEFT_TOP_POINT, leftTopPoint); squareMap.put(DistanceProperty.RIGHT_TOP_POINT, rightTopPoint); squareMap.put(DistanceProperty.LEFT_BOTTOM_POINT, leftBottomPoint); squareMap.put(DistanceProperty.RIGHT_BOTTOM_POINT, rightBottomPoint); return squareMap; } /** * 计算一个经纬度在指定距离范围内的最大经纬度和最小经纬度 * @param lng * @param lat * @param distance * @return Double[0] 最大经度 Double[1] 最小经度 Double[2] 最大纬度 Double[3] 最小纬度 */ public static Double[] getScopeGps(double lng, double lat, int distance){ //每100米,经纬度的差值 double lngPI =0.0009788;//经度 double latPI = 0.0009;//纬度 double maxResultLng = lng+distance/100*lngPI; double minResultLng = lng-distance/100*lngPI; double maxResultLat = lat+distance/100*latPI; double minResultLat = lat-distance/100*latPI; Double[] result =new Double[4]; result[0]=minResultLng; result[1]=maxResultLng; result[2]=minResultLat; result[3]=maxResultLat; return result; } public static void main(String[] args) { int test = getDistance( 113.947971,22.554800,113.948654,22.553683); Double[] aa =getScopeGps(113.947875,22.555001,500); System.out.println(aa[0]); System.out.println(aa[1]); System.out.println(aa[2]); System.out.println(aa[3]); System.out.println(test); } }
文章来源: 计算两个点的经纬度的距离