美文网首页
小程序地图逆地址解析

小程序地图逆地址解析

作者: 秀萝卜 | 来源:发表于2022-07-12 09:31 被阅读0次

公司需求:获取经纬度和地址名称。地址名称需要用腾讯地图的逆地址解析
官方文档:https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/methodReverseGeocoder
参考资料:https://blog.csdn.net/ZhuYuanLe/article/details/119862388

import QQMapWX from '../../utils/qqmap-wx-jssdk.min.js'
var qqmapsdk
Page({
    data: {
        latitude: '',
        longitude: '',
        address: '',
    },
    onLoad: function (options) {
        qqmapsdk = new QQMapWX({
            key:"KTABZ-*****-*****-*****-*****-DIBST"
        });
        this.getPosition()
    },
    //获取位置
    getPosition() {
        var that = this
        wx.getLocation({
            type: 'gcj02', //返回可以用于 wx.openLocation 的经纬度
            success(res) {
                const latitude = res.latitude //维度
                const longitude = res.longitude
                that.setData({
                    latitude: latitude,
                    longitude: longitude
                })
                that.getAddress()
            }
        })
    },
    //逆地址解析
    getAddress() {
        var that = this
        qqmapsdk.reverseGeocoder({
            location: {
                latitude: that.data.latitude,
                longitude: that.data.longitude
            },
            get_poi: 1,
            poi_options: 'policy=2;radius=3000;page_size=20;page_index=1',
            success: function (res) {
                console.log(res);
                that.setData({
                    address: res.result.address
                })
            },
            fail: function (res) {
                console.log(res);
            },
            complete: function (res) {
                console.log(res);
            }
        })
    }
    // 这里是pc端可用的方法。刚开始没看到,腾讯地图给小程序专门提供了方法。
    // 当然在小程序也是可以用的。测试的时候是可以的。
    // 如果报错,IP未被授权,解决方法见:https://lbs.qq.com/faq/serverFaq/webServiceKey。
    // getAddress() {
    //     var locationString = this.data.latitude + "," + this.data.longitude;
    //     wx.request({
    //         url: 'http://apis.map.qq.com/ws/geocoder/v1/?l&get_poi=1',
    //         data: {
    //             "key": "KTABZ-*****-*****-*****-*****-DIBST",
    //             "location": locationString
    //         },
    //         method: 'GET',
    //         success: function (r) {
    //             console.log(r);
    //         }
    //     })
    // }
})

相关文章

网友评论

      本文标题:小程序地图逆地址解析

      本文链接:https://www.haomeiwen.com/subject/gejnbrtx.html