用Haversine来就算两个经纬度之间的路面距离:
[b]1. 标准公式[/b]
[b]2. 变形公式:[/b]
好像变形公式才能正确计算,不知道是不是Mysql的函数实现的问题,留作待解决问题。
[b]1. 标准公式[/b]
// 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 + ")";
[b]2. 变形公式:[/b]
// 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的函数实现的问题,留作待解决问题。