12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package cn.com.ty.lift.common.utils;
- /**
- * 通过地图上的两个坐标计算距离(Java版本)
- * Add by 成长的小猪(Jason.Song) on 2017/11/01
- * http://blog.csdn.net/jasonsong2008
- */
- public class MapHelper {
- /**
- * 经纬度转化成弧度
- * Add by 成长的小猪(Jason.Song) on 2017/11/01
- * http://blog.csdn.net/jasonsong2008
- *
- * @param d 经度/纬度
- * @return
- */
- private static double rad(double d) {
- return d * Math.PI / 180.0;
- }
- /**
- * 计算两个坐标点之间的距离
- * Add by 成长的小猪(Jason.Song) on 2017/11/01
- * http://blog.csdn.net/jasonsong2008
- *
- * @param firstLatitude 第一个坐标的纬度
- * @param firstLongitude 第一个坐标的经度
- * @param secondLatitude 第二个坐标的纬度
- * @param secondLongitude 第二个坐标的经度
- * @return 返回两点之间的距离,单位:公里/千米
- */
- public static double getDistance(double firstLatitude, double firstLongitude,
- double secondLatitude, double secondLongitude) {
- double firstRadLat = rad(firstLatitude);
- double firstRadLng = rad(firstLongitude);
- double secondRadLat = rad(secondLatitude);
- double secondRadLng = rad(secondLongitude);
- double a = firstRadLat - secondRadLat;
- double b = firstRadLng - secondRadLng;
- //地球半径
- double earthRadius = 6378.137;
- double cal = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(firstRadLat)
- * Math.cos(secondRadLat) * Math.pow(Math.sin(b / 2), 2))) * earthRadius;
- return Math.round(cal * 10000d) / 10000d;
- }
- /**
- * 计算两个坐标点之间的距离
- * Add by 成长的小猪(Jason.Song) on 2017/11/01
- * http://blog.csdn.net/jasonsong2008
- *
- * @param firstPoint 第一个坐标点的(纬度,经度) 例如:"31.2553210000,121.4620020000"
- * @param secondPoint 第二个坐标点的(纬度,经度) 例如:"31.2005470000,121.3269970000"
- * @return 返回两点之间的距离,单位:公里/千米
- */
- public static double GetPointDistance(String firstPoint, String secondPoint) {
- String[] firstArray = firstPoint.split(",");
- String[] secondArray = secondPoint.split(",");
- double firstLatitude = Double.parseDouble(firstArray[0].trim());
- double firstLongitude = Double.parseDouble(firstArray[1].trim());
- double secondLatitude = Double.parseDouble(secondArray[0].trim());
- double secondLongitude = Double.parseDouble(secondArray[1].trim());
- return getDistance(firstLatitude, firstLongitude, secondLatitude, secondLongitude);
- }
- }
|