微信小程序获取手机定位+经纬度转详细地址

在微信小程序中获取手机定位信息

 wx.getLocation({
            type: 'wgs84',
            success(res) {
                // console.log(res)
                const latitude = res.latitude
                const longitude = res.longitude
                if (flag) {
                    getMinKey(_this, latitude, longitude, callback, errback)
                }
            },
            fail(res) {
                // console.log(res)
                if (res.errCode == 2) {
                    common.getToast('请打开手机定位授权');
                }
            },
        })

官方文档给出的wx.getLocation只能获取到当前位置的经纬度,没有给出当前位置的具体信息,这时需要将经纬度转换为详细地址。

腾讯位置服务为微信小程序提供了基础的标点能力、线和圆的绘制接口等地图组件和位置展示、地图选点等地图API位置服务能力支持,使得开发者可以自由地实现自己的微信小程序产品。

配置可参考:https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/jsSdkOverview

使用:

location-address.js

//获取定位地址,通过经纬度转成详细地址
const app = getApp();
const common = require('./common.js');
const dataMainModule = require('./main.js');
// 引入SDK核心类,js文件根据自己业务,位置可自行放置
const QQMapWX = require('./qqmap-wx-jssdk.min.js');
let isWaitTenSeconds = false; //等待10秒如果定位异常置为true
let addrNum = 0;
/**
 * 获取当前定位信息
 * @param flag  是否返回经纬度转换地址详细信息
 * @returns Boolean
 */
const currentLocation = (_this, flag = false, callback, errback) => {
    let that = _this;
    var opt = {
        scope: 'scope.userLocation',
        content: '获取您的手机定位位置'
    }
    app.getSettingRecord(opt).then((data) => {
        wx.getLocation({
            type: 'wgs84',
            success(res) {
                // console.log(res)
                const latitude = res.latitude
                const longitude = res.longitude
                if (flag) {
                    getMinKey(_this, latitude, longitude, callback, errback)
                }
            },
            fail(res) {
                // console.log(res)
                if (res.errCode == 2) {
                    common.getToast('请打开手机定位授权');
                }
            },
        })
    })
}

/**
 * 经纬度转换成详细地址
 * @param _this  页面作用域
 * @param latitude  经度
 * @param longitude  纬度
 * @returns Boolean
 */
const getMinKey = (_this, latitude, longitude, callback, errback) => {
    let that = _this;
    //var key = '7IABZ-XBRRT-C4OXJ-VG2LS-EOBG5-4JFG4';        
    dataMainModule.getMinKey({
        type: 1
    }, (res) => {
        // console.log('获取定位key', res)
        if (res.retCode == 200 && res.mapKey) {
            var mapKey = res.mapKey,
                retryCount = res.retryCount;
            mapKey = mapKey.replace(/^"|"$/g, '');
            var qqmapsdk = new QQMapWX({
                key: mapKey
            });
            var params = {
                latitude: latitude,
                longitude: longitude
            };
            qqmapsdk.reverseGeocoder({
                location: params,
                success: function (res1) {
                    // console.log(res1);
                    if (res1) {
                        addrNum = 0;
                        isWaitTenSeconds = false;
                        if (callback) {
                            callback(res1.result)
                        }
                    }
                },
                fail: function (res) {
                    // console.log(res);
                    addrNum++;
                    common.log.warn("获取定位地址失败", JSON.stringify({
                        errMsg: JSON.stringify(res),
                        mapKey: mapKey
                    }));
                    common.log.setFilterMsg("getMinKey");
                    //按后台返回次数尝试重新获取
                    if (addrNum <= retryCount) {
                        setTimeout(() => {
                            getMinKey(_this, latitude, longitude, callback, errback)
                        }, 2000)
                    }
                    if (errback) {
                        errback(res)
                    }
                }
            })
        } else {
            // wx.removeStorageSync('zbg_standard_Address');
            if (errback) {
                errback(res)
            }
        }
    }, (res) => {
        // console.log(res)
        //common.getToast(res);
        setTimeout(() => {
            isWaitTenSeconds = true
        }, 15000)
        if (errback) {
            errback(res)
        }
    })
}

module.exports = {
    currentLocation,
    getMinKey
}

页面使用:

getmobileSeat() { //获取手机定位位置
        let that =this;
        locationAddress.currentLocation(this, true,(res)=>{
            if(res){
                var address = res.formatted_addresses?.recommend;
                console.log(address)
                that.setData({
                    mobileSeat:address
                })
            }
        });
    },

经纬度转换详细地址数据

 

posted @ 2024-05-15 10:54  时光独醒  阅读(415)  评论(0编辑  收藏  举报