计算原理:根据勾股定理算两个坐标直线距离
java版本
package com.application.util; /** * 地图坐标距离计算 * */ public class LocationUtils { private static double EARTH_RADIUS = 6378.137; private static double rad(double d) { return d * Math.PI / 180.0; } /** * 通过经纬度获取距离(单位:米) * @param lng1 经度1 * @param lat1 纬度1 * @param lng2 经度2 * @param lat2 纬度2 * @return */ 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) - 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; s = Math.round(s * 10000d) / 10000d; s = s*1000; return s; } public static void main(String[] args){ double lng1 = 116.434395; double lat1 = 39.862268; double lng2 = 116.434651; double lat2 = 39.862542; double distance = getDistance(lng1,lat1,lng2,lat2); System.out.println("distanc:"+distance); } }
sql版本
SELECT *, ROUND( 6378.138 * 2 * ASIN( SQRT( POW( SIN( ( 39.862542 * PI() / 180 - y_latitude * PI() / 180 ) / 2 ), 2 ) + COS(39.862542 * PI() / 180) * COS(y_latitude * PI() / 180) * POW( SIN( ( 116.434651 * PI() / 180 - x_longitude * PI() / 180 ) / 2 ), 2 ) ) ) * 1000 ) AS juli FROM t_organ ORDER BY juli ASC
延伸阅读: