分享移动端获取当前经纬度并转换详细位置的方法(h5可用)

uniapp使用腾讯地图key获取当前经纬度并且转换成详细位置(需收费)

腾讯地图key申请官网

腾讯位置服务 - 立足生态,连接未来

具体申请方法可参考

h5平台腾讯定位地图应用key申请的方法_pr.map.qq.comz找key_赶一场落日的博客-CSDN博客

配置项

h5对应配置(其他平台自行配置)

使用uni官方接口获取当前经纬度(此处是获取当前经纬度方法)

Location() {
                let this_ = this
                uni.getLocation({
                    type: 'wgs84',
                    success: function(res) {
                        this_.formData.latitude = res.latitude
                        this_.formData.longitude = res.longitude
                        this_.getLocation(res.latitude, res.longitude)
                    },
                    fail: function(err) {
                        console.log(err, 'err');
                    }
                });

            },

使用经纬度转换详细位置(使用腾讯接口)

    getLocation(lat, lon) {
                let this_ = this
                var url = 'https://apis.map.qq.com/ws/' + '/geocoder/v1/?location=' + lat + ',' + lon+
                    '&key=' + 腾讯申请到的key
                uni.request({
                    url: url,
                    method: 'get',
                    success: (result) => {
                        this_.formData.translates = result.data.result.address
                    },
                    fail(res) {
                        console.log('失败了', res, url);
                    },
                });
            },

前端解决线上跨域

到线上使用 https://apis.map.qq.com/会跨域,如运维不想配置跨域前端解决方案(肯定有影响目前未发现可正常使用)

使用组件 vue-jsonp

下载按照

npm i vue-jsonp

使用方法

引入

import { jsonp } from 'vue-jsonp'

let url = 'https://apis.map.qq.com/ws/geocoder/v1/'

jsonp(url, {
                    key: TXKYE,//腾讯key
                    location: lat + ',' + lon,//经纬度
                    output: 'jsonp'
                }).then(res => {
                    this.formData.address = res.result.formatted_addresses.recommend
                    this.$toast.clear();
                })

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值