Ruby 计算经纬度之间距离

来源:互联网 发布:淘宝助理与千牛的区别 编辑:程序博客网 时间:2024/06/07 09:34

代码经过网上公式coding出来的。算是转载吧。但真心忘记是哪里抄来的了…


lat1 = 39.993956lng1 = 116.173553lat2 = 39.952912lng2 = 116.29715include Mathlat_diff = (lat1 - lat2)*PI/180.0lng_diff = (lng1 - lng2)*PI/180.0lat_sin = Math.sin(lat_diff/2.0) ** 2lng_sin = Math.sin(lng_diff/2.0) ** 2first = Math.sqrt(lat_sin + Math.cos(lat1*PI/180.0) * Math.cos(lat2*PI/180.0) * lng_sin)result = Math.asin(first) * 2 * 6378137.0p result.to_i