使用uniapp获取当前位置

1.引入文件

import QQMapWX from "../../utils/qqmap-wx-jssdk.js"

下载地址: 微信小程序JavaScript SDK | 腾讯位置服务

2.获取位置信息

// 获取位置信息
            getLocationInfo() {
                return new Promise((resolve) => {
                    //位置信息默认数据
                    let location = {
                        longitude: 0,
                        latitude: 0,
                        province: "",
                        city: "",
                        area: "",
                        street: "",
                        address: "",
                    };
                    uni.getLocation({
                        type: "gcj02",
                        success(res) {
                            location.longitude = res.longitude;
                            location.latitude = res.latitude;
                            // 腾讯地图Api
                            const qqmapsdk = new QQMapWX({
                                key: '' //申请的key 用自己的
                            });
                            qqmapsdk.reverseGeocoder({
                                location,
                                success(response) {
                                    console.log('response', response)
                                    let info = response.result;
                                    location.province = info.address_component.province;
                                    location.city = info.address_component.city;
                                    location.area = info.address_component.district;
                                    location.street = info.address_component.street;
                                    location.address = info.address;
                                    resolve(location);
                                },
                            });
                        },
                        fail(err) {
                            resolve(location);
                        },
                    });
                });
            },

3. then返回省市区值

this.getLocationInfo().then(res => {

this.city_name = res.province //省

this.city_city = res.city //市

this.city_area = res.area //区

this.address = this.city_area ? this.city_area : this.city_city ? this.city_city : this.city_name

})

注:腾讯地图的key申请地址

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值