微信小程序地图定位

文章描述了一个使用WXML和JavaScript编写的组件,用于显示两点之间的距离并实现点击后在地图上跳转。组件包含方法如`getLocationAndDistance`获取用户位置并计算与标记点的距离,`markerTap`事件触发地图App的导航功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现定位距离,点击跳转地图

做成组件方便调用

wxml

<view class="distance-navigator" style="{{distanceNavStyle}}"  bindtap="markerTap">
    <text class="distance">{{distance}}KM</text>
</view>

js

 properties: {
        marker: {
            type: Object
        },
        showNav: {
            type: Boolean,
            value: true
        },
        distanceNavStyle: {
            type: String,
            value: 'bottom: 0; right: 30rpx;'
        }
    },



   /**
     * 组件的方法列表
     */
    methods: {
        markerTap(e) {
          console.log( this.data.marker,'000');
          const latitude = Number(this.data.marker.latitude);
          const longitude = Number(this.data.marker.longitude);
            // 获取标记信息
            // const {latitude, longitude} = this.data.marker
        
            // 打开地图 App 进行导航
            wx.openLocation({
                latitude,
                longitude
            });
            console.log('---000');
        },
        getLocationAndDistance () {

            let that = this;
            const {latitude: targetLatitude, longitude: targetLongitude} = that.data.marker;
            try {
                wx.getFuzzyLocation({
                    type: 'wgs84', // 使用 GPS 坐标
                    success(res) {
                        // 这里可以将用户的位置坐标保存到数据中或进行其他操作
                        const userLatitude = res.latitude; // 用户当前纬度
                        const userLongitude = res.longitude; // 用户当前经度
                
                        that.getDistance(userLatitude, userLongitude, targetLatitude, targetLongitude);
                    },
                    fail(err) {
                        // 获取位置失败时的处理逻辑
                        console.error('获取位置失败', err);
                    }
                });
            } catch {
                // 获取不到经纬度时默认北京
                const userLatitude = 39.90965; //纬度
                const userLongitude = 116.40418; //经度

                that.getDistance(userLatitude, userLongitude, targetLatitude, targetLongitude);
            };
        },
        getDistance (lat1, lon1, lat2, lon2) {
            const R = 6371; // 地球的半径,单位千米
            const dLat = this.deg2rad(lat2 - lat1);
            const dLon = this.deg2rad(lon2 - lon1);
            
            const a =
                Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.cos(this.deg2rad(lat1)) * Math.cos(this.deg2rad(lat2)) *
                Math.sin(dLon / 2) * Math.sin(dLon / 2);
            
            const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
            const distance = R * c; // 距离(单位:千米)
            this.setData({
                distance: distance.toFixed(2)
            });
            return distance;
        },
        deg2rad (deg) {
            return deg * (Math.PI / 180);
        }
    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值