通过地图上的两个经纬度坐标计算距离

package com.until;
/** 
 * @author xml
 * 通过地图上的两个坐标计算距离
 */  
public class CoordinateDistance {
	 /** 
     * 地球半径 
     */  
    private static double EarthRadius = 6378.137;  
  
    /** 
     * 经纬度转化成弧度 
     * 
     * @param d 经度/纬度 
     * @return 度数×π÷180
     */  
    private static double rad(double d) {  
        return d * Math.PI / 180.0;  
    }  
  
    /** 
     * 计算两个坐标点之间的距离 
     * 
     * @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;  
    }  
  
    /** 
     * 计算两个坐标点之间的距离 
     * 
     * @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);  
    }  
    

}

测试:

public class CoordinateDistanceTest {


	 @Test  
	    public void getDistance() throws Exception {  
	        /** 
	         * 第一种方法 
	         */  
	        double result = CoordinateDistance.getDistance(116.604412,40.079641,116.390865,39.782678);  
	        System.out.println(result);  
	        double expected = 14.2243;  
	        TestCase.assertEquals(expected, result);  
	    }  
	  
	    @Test  
	    public void getPointDistance() throws Exception {  
	        /** 
	         * 第二种方法 
	         */  
	        double result = CoordinateDistance.GetPointDistance("116.604412,40.079641", "116.390865,39.782678");  
	        System.out.println(result);  
	        double expected = 14.2243;  
	        TestCase.assertEquals(expected, result);  
	    }  
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT界的一只菜鸟

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值