计算经纬度之间的地球面距离_Mysql实现

用Haversine来就算两个经纬度之间的路面距离:

1. 标准公式

// The unit of earth radius is km

private static final String EARTH_RADIUS = "6371";

String FirstStep = "(POW(SIN(({1} - {2}) / 2), 2) + COS({1}) * COS({2}) * POW(SIN(({3} - {4}) / 2), 2))";
String SecondStep = "ASIN(SQRT(" + FirstStep + "))";
String HaversineFormula = "(2 * " + EARTH_RADIUS + " * " + SecondStep + ")";
String nearBy = " and ({0} >= " + HaversineFormula + ")"; 


2. 变形公式:

// The unit of earth radius is km

private static final String EARTH_RADIUS = "6371";

String FirstStep = "POW(SIN(PI() * ({1} - {2}) / 360), 2) + COS(PI() * {1} / 180) * COS({2} * PI() / 180) * POW(SIN(PI() * ({3} - {4}) / 360), 2)";
String SecondStep = "ASIN(SQRT(" + FirstStep + "))";
String HaversineFormula = "(2 * " + EARTH_RADIUS + " * " + SecondStep + ")";
String nearBy = " and ({0} >= " + HaversineFormula + ")";


好像变形公式才能正确计算,不知道是不是Mysql的函数实现的问题,留作待解决问题。

你可能感兴趣的:(java,mysql,360)