小程序---获取当前定位的经纬度

本文介绍如何使用微信小程序JavaScript SDK实现用户地理位置的获取与逆地理编码,即从经纬度坐标转换为具体的地址描述,包括城市名等信息。

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

需要用到微信小程序JavaScript SDK:

入门及使用限制

逆地址解析(坐标位置描述)

    // 获取当前定位的经纬度
    getLocation() {
      debugger
      let that = this
      wx.getSetting({
        success: (res) => {
          if (!res.authSetting['scope.userLocation']) {
            wx.getLocation({
              type: 'wgs84',
              success: function (res) {
                qqmapsdk = new QQMapWX({
                  key: '…………' //这里用自己的key秘钥进行填充
                });
                that.getLocal(res.latitude, res.longitude)
              },
              fail() { // 拒绝获取地理位置
                wx.showModal({
                  content: '您暂未开启地理位置权限,是否开启',
                  confirmText: '去开启',
                  cancelText: '取消',
                  success(res) {
                    if (res.confirm) {
                      wx.openSetting({
                        scope: 'scope.userLocation',
                        success(res) {
                          wx.getLocation({
                            type: 'wgs84',
                            success: function (res) {
                              qqmapsdk = new QQMapWX({
                                key: '…………' //这里用自己的key秘钥进行填充
                              });
                              that.getLocal(res.latitude, res.longitude)
                            },
                          })
                        },
                        fail() {
                          wx.showToast({
                            title: res,
                            icon: 'none',
                            duration: 2000
                          })
                        }
                      })
                    } else if (res.cancel) {
                      wx.showToast({
                        title: '你已拒绝获取地理位置信息',
                        icon: 'none',
                        duration: 2000
                      })
                      that.setData({
                        showTab: false
                      })
                    }
                  }
                })
              }
            })
          } else {
            wx.getLocation({
              type: 'wgs84',
              success: function (res) {
                qqmapsdk = new QQMapWX({
                  key: '…………' //这里用自己的key秘钥进行填充
                });
                that.getLocal(res.latitude, res.longitude)
              },
            })
          }
        },
      })
    },
    // 获取当前城市名字
    getLocal: function (latitude, longitude) {
      let that = this;
      //位置坐标
      qqmapsdk.reverseGeocoder({
        location: {
          latitude: latitude,
          longitude: longitude
        },
        success: function (res) {
          that.setData({
            locationCity: res.result.address_component.city.split('市')[0],
			……
          })
          that.getProjectList(res.result.address_component.city.split('市')[0])
        },
        fail: function (res) {
          that.setData({
            locationCity: '获取地理位置失败'
          })
        }
      });
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值