通过经纬度计算距离

MyBatis
cast(ACOS(SIN((#{Latitude}*3.1415) /180)SIN((LATITUDE * 3.1415) /180)
cos((#{latitude} * 3.1415)/180)COS((LATITUDE * 3.1415) /180)cos((#{Longitude} * 3.1415)180(LONGITUDE * 3.1415)/180))* 6378137 as int) as M
Java
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
calculateDistance( lat2, lon2) {
            const lat1 = uni.getStorageSync('latitude')
            const lon1 = uni.getStorageSync('longitude')

            console.log(lat1,lon2)

            const R = 6371; // 地球半径,单位为千米
            const dLat = (lat2 - lat1) * (Math.PI / 180);
            const dLon = (lon2 - lon1) * (Math.PI / 180);
            const a =
                Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.cos(lat1 * (Math.PI / 180)) *
                Math.cos(lat2 * (Math.PI / 180)) *
                Math.sin(dLon / 2) *
                Math.sin(dLon / 2);
            const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
            const distance = R * c;
            return distance;
        },

通过经纬度计算距离
https://guiyunweb.com/archives/tong-guo-jing-wei-du-ji-suan-ju-li
作者
归云
发布于
2023年10月07日
更新于
2024年06月18日
许可协议