Created
October 18, 2017 10:47
-
-
Save kinginblue/b71c2617af6ec40565639827f76e5c3b to your computer and use it in GitHub Desktop.
经纬度计算器
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 经纬度计算器 | |
*/ | |
public class LatLonCalculator { | |
/** | |
* 一纬度的距离约为:111.7 km | |
*/ | |
public static final BigDecimal KM_OF_ONE_LAT = new BigDecimal(111.7); | |
/** | |
* 求纬度差之间的距离 | |
* @param latDiffer 纬度差 | |
* @return 纬度差之间的距离,单位 km | |
*/ | |
public static BigDecimal kmDistanceOfLatDiffer(BigDecimal latDiffer) { | |
/** | |
* 111.7 * latDiffer.abs() | |
*/ | |
return KM_OF_ONE_LAT | |
.multiply(latDiffer.abs()) | |
.setScale(2, RoundingMode.HALF_EVEN); | |
} | |
/** | |
* 求经度差之间的距离 | |
* @param currentLat 当前纬度,不同纬度上,经度差距离不一样! | |
* @param lonDiffer 经度差 | |
* @return 经度差之间的距离,单位 km | |
*/ | |
public static BigDecimal kmDistanceOfLonDiffer(BigDecimal currentLat, BigDecimal lonDiffer) { | |
/** | |
* 111.7 * cos(currentLat).abs() * lonDiffer.abs() | |
*/ | |
return KM_OF_ONE_LAT | |
.multiply(new BigDecimal(Math.cos(currentLat.doubleValue())).abs()) | |
.multiply(lonDiffer.abs()) | |
.setScale(2, RoundingMode.HALF_EVEN); | |
} | |
/** | |
* 求距离对应的纬度差 | |
* @param distance 距离,单位 km | |
* @return 纬度差 | |
*/ | |
public static BigDecimal latDifferOfKmDistance(BigDecimal distance) { | |
/** | |
* distance / 111.7 | |
*/ | |
return distance | |
.divide(KM_OF_ONE_LAT, 6, RoundingMode.HALF_EVEN); | |
} | |
/** | |
* 求距离对应的经度差 | |
* @param currentLat 当前纬度,不同纬度上,相同距离的经度差不一样! | |
* @param distance 距离,单位 km | |
* @return 经度差 | |
*/ | |
public static BigDecimal lonDifferOfKmDistance(BigDecimal currentLat, BigDecimal distance) { | |
/** | |
* 当前纬度下,1经度距离 | |
*/ | |
BigDecimal distanceOfOneLon = kmDistanceOfLonDiffer(currentLat, BigDecimal.ONE); | |
/** | |
* distance / distanceOfOneLon 按比例求得距离对应的经度差 | |
*/ | |
return distance | |
.divide(distanceOfOneLon, 6, RoundingMode.HALF_EVEN); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks