通过两个位置的经纬度坐标计算距离(Java版本)

目前移动领域的项目越来越多,最近自己带领团队也着手移动端项目的开发,在开发中经常涉及到地理位置的业务,获取移动端客户GPS坐标位置来计算距离,一开始使用的是百度地图API来计算,发现百度API只支持50位置坐标计算,对于真正的业务上的位置点,这远远不够,后来谷歌提供一种计算公式,可以很快的计算出两个坐标之间的距离,这在一定程度上帮助我们快速的计算出距离,感觉很不错,将代码分享给有需要的同学。

以下展示是Java代码
如果你使用的C#,请访问这个地址
 http://blog.csdn.net/jasonsong2008/article/details/78423496

/**
 * 通过地图上的两个坐标计算距离(Java版本)
 * Add by 成长的小猪(Jason.Song) on 2017/11/01
 * http://blog.csdn.net/jasonsong2008
 */
public class MapHelper {
    /**
     * 地球半径
     */
    private static double EarthRadius = 6378.137;

    /**
     * 经纬度转化成弧度
     * 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 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;
        double result = Math.round(cal * 10000d) / 10000d;
        return result;
    }

    /**
     * 计算两个坐标点之间的距离
     * 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.valueOf(firstArray[0].trim());
        double firstLongitude = Double.valueOf(firstArray[1].trim());
        double secondLatitude = Double.valueOf(secondArray[0].trim());
        double secondLongitude = Double.valueOf(secondArray[1].trim());
        return getDistance(firstLatitude, firstLongitude, secondLatitude, secondLongitude);
    }


}


呵呵!调用方法应该不用说了吧,我还是帖出来吧

/**
 * 文章来源于成长的小猪
 * http://blog.csdn.net/jasonsong2008
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/applicationContext.xml"})
public class MapHelperTest {
    @Test
    public void getDistance() throws Exception {
        /**
         * 第一种调用方法
         */
        double result = MapHelper.getDistance(31.2553210000, 121.4620020000, 31.2005470000, 121.3269970000);
        System.out.println(result);
        double expected = 14.2243;
        TestCase.assertEquals(expected, result);
    }

    @Test
    public void getPointDistance() throws Exception {
        /**
         * 第二种调用方法
         */
        double result = MapHelper.GetPointDistance("31.2553210000,121.4620020000", "31.2005470000,121.3269970000");
        System.out.println(result);
        double expected = 14.2243;
        TestCase.assertEquals(expected, result);
    }

}


猜你喜欢

转载自blog.csdn.net/jasonsong2008/article/details/78434237
今日推荐