李好坏
李好坏
  • 发布:2023-02-14 23:45
  • 更新:2023-02-16 09:09
  • 阅读:220

求 uni.getLocation 定位计算两地之间的距离案例

分类:uni-app

使用这个
distance(la1, lo1, la2, lo2) {
var La1 = la1 Math.PI / 180.0;
var La2 = la2
Math.PI / 180.0;
var La3 = La1 - La2;
var Lb3 = lo1 Math.PI / 180.0 - lo2 Math.PI / 180.0;
var s = 2 Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) +Math.cos(La1) Math.cos(La2) Math.pow(Math.sin(Lb3 / 2), 2)));
s = s
6378.137;//地球半径
s = Math.round(s * 10000) / 10000;
console.log("计算结果",s,'KM');
},
计算出两地之间有6千多公里,真实距离只有2公里。

这是我的大概代码

async init() { //页面初始化
console.log('页面初始化')

            this.loading = true;  
            let error = {}, res = this.location;  
            let lat = '111.985387';//这是店铺的 经度:  
            let lng = '27.706344';//这是店铺的 经度:  

             //关闭定位功能 ,因为加载太慢,影响用户体验  

            if (!this.location.hasOwnProperty('latitude')) {  
                console.log('因为加载太慢,影响用户体验');  
                 [error, res] = await uni.getLocation({  
                    type: 'wgs84',  
                    altitude:true  
                });  
                if (error) {  
                    this.$api.msg('获取定位失败');  
                    this.$refs.uToast.show({  
                        title: '获取位置失败,请检查是否开启相关权限',  
                        type: 'error'  
                    });  
                    // 默认地为你为北京地址  
                    res = {  
                        latitude: 39.919990,  
                        longitude: 116.456270  
                    };   
                }  

        if (res) {  
                console.log('当前位置的经度1-:' + res.longitude);  
                console.log('当前位置的纬度-:' + res.latitude);  
                this.SET_LOCATION(res); //定位功能  

            let shop_id = 0; //店铺ID 0 表示所有店铺,这里使用的是默认全部店铺  

                let shop = await this.$api.request('/shop/nearby', 'POST', {  

                    lat : '111.99173',//这是店铺的 经度:  
                    lng : '27.712249',//这是店铺的 经度:  
                    shop_id : shop_id,  

                });  

                console.log('lat',lat);  
                console.log('lng',lng);  

                console.log('res.latitude--2',res.latitude);  
                console.log('res.longitude--2',res.longitude);  

              this.distance(lat,lng,res.latitude,res.longitude,); //计算定位远近  

            }  
        },  

        //计算两个地方的距离【定位】   
   distance(la1, lo1, la2, lo2) {    
               var La1 = la1 * Math.PI / 180.0;    
               var La2 = la2 * Math.PI / 180.0;    
               var La3 = La1 - La2;    
               var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;    
               var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) +Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));    
               s = s * 6378.137;//地球半径    
               s = Math.round(s * 10000) / 10000;    
               console.log("计算结果",s,'KM');     
           },
2023-02-14 23:45 负责人:无 分享
已邀请:
亦春亦秋

亦春亦秋 - 用爱发电

// 计算两点之间直线距离
export function algorithm(point1, point2) {
let [x1, y1] = point1;
let [x2, y2] = point2;
let Lat1 = rad(x1); // 纬度
let Lat2 = rad(x2);
let a = Lat1 - Lat2;// 两点纬度之差
let b = rad(y1) - rad(y2); // 经度之差
let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)

  • Math.cos(Lat1) Math.cos(Lat2) Math.pow(Math.sin(b / 2), 2)));
    // 计算两点距离的公式
    s = s 6378137.0;// 弧长等于弧度乘地球半径(半径为米)
    s = Math.round(s
    10000) / 10000;// 精确距离的数值
    return s;

}

// 角度转换成弧度
const rad =(d) =>{
return d * Math.PI / 180.00;
};

李好坏

李好坏 (作者) - 喜欢编程

point1
point2

这两个是经度还是纬度

亦春亦秋

亦春亦秋 - 用爱发电

point1 【纬度,经度】 A点
point2 【纬度,经度】B点

这个是用来求两个经纬度之间的距离的

要回复问题请先登录注册