学着做项目需要根据很据给出的两点经纬度信息计算两地的实际距离,查了查,有公式奉上:
google地图提供的方法:
相关c#代码如下
private static double rad(double d) { return d * Math.PI / 180.0; } //根据两点的经纬度计算两地距离 public static double GetDistance(double lon1, double lat1, double lon2, double lat2) { double radLat1 = rad(lat1); double radLat2 = rad(lat2); double a = radLat1 - radLat2; double b = rad(lon1) - rad(lon2); double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2))); s = s * EARTH_RADIUS; s = Math.Round(s * 10000) / 10000; return s; }
js代码如下:
function rad(d){ return d*Math.PI/180.0; } function distance(lon,lat,lon1,lat1){ // lon1=108; lat1=34.0; // lon2=108; lat2=35.0; var radLat1 = rad(lat1); var radLat2 = rad(lat2); var a = radLat1 - radLat2; var b = rad(lon1) - rad(lon2); var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2)+Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)))*6378.137; return s; }
实测精确度比较还算比较高,很有用,记下来。