uni获取当前定位和计算距离

该博客展示了一个uni-app小程序中获取用户经度和纬度的示例,并利用高德地图API进行逆地理编码获取位置名称。同时,实现了计算当前位置与指定坐标之间的距离功能,结果以米为单位。在onLoad函数中调用了getLocation和distance方法,确保页面加载时获取并显示相关信息。
<template>
    <view class="content">
        <view>经度:{{this.longitude}}</view>
        <view>纬度:{{latitude}}</view>
        <view>名称:{{pocationName}}</view>
        <view>距离:{{s}}</view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                longitude:'',
                latitude:'',
                pocationName:'',
                s:'',
            }
        },
        onLoad() {
        
            this.getLocation()
            this.distance(39.901403,116.406243,30.53599,104.096219)
        },
        methods: {
            getLocation(){
                let this_=this
                uni.getLocation({
                    type: 'gcj02', //返回可以用于uni.openLocation的经纬度
                    success:(res)=>{
                
                        this_.latitude = res.latitude;
                        this_.longitude = res.longitude;
                        console.log(res)
                        uni.request({
                            header:{
                                "Content-Type": "application/text"
                            },
                            //注意:这里的key值需要高德地图的 web服务生成的key  只有web服务才有逆地理编码
                            url:'https://restapi.amap.com/v3/geocode/regeo?output=JSON&location='+res.longitude+','+res.latitude+'&key=地图密钥',
                            success(re) {
                                console.log(re,11111)
                                if(re.statusCode===200){
                                    this_.pocationName=re.data.regeocode.formatted_address
                                    
                                }else{
                                    console.log("获取信息失败,请重试!")
                                }
                             }
                        });
                    }
                });
                
            },
            //1当前位置 2目标位置
            distance(lat1, lng1, lat2, lng2) {
                var radLat1 = lat1 * Math.PI / 180.0;
                var radLat2 = lat2 * Math.PI / 180.0;
                var a = radLat1 - radLat2;
                var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
                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)));
                s = s * 6378.137; // EARTH_RADIUS;
                s = Math.round(s * 10000) / 10000;
                if (s < 1) {
                    this.s = s * 1000 + 'm'
            
                } else {
                    this.s = s + 'm'
                }
            
            },
        }
    }
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值