最近搜索

小程序 获取 用户的位置 信息

浏览:7
管理员 2025-06-23 10:38



app.json 添加支持

  "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息将用于,订单创建"
    }
  },
  "requiredPrivateInfos": ["getLocation"]

image.png

image.png









获取当前 经纬度  

  load_pos() {
    var this_ = this;
    wx.getLocation({
     //type: 'gcj02', 这个参数 看一下干什么用的。
      success: function (res) {
        console.log("位置数据:", res);
        this_.setData({
          lng: res.longitude,
          lat: res.latitude
        })
      },
      fail(err) {
        console.error('定位失败:', err.errMsg);
        wx.showModal({
          title: '权限提示',
          content: '需要位置权限才能提供服务',
          success(res) {
            if (res.confirm) wx.openSetting();//跳转小程序设置界面  就是 位置和订阅消息 设置那个界面
          }
        });
      }
    })
  },

image.png

//或者保存 经纬度到订单,我们可以通过 我们系统的腾讯地图 打开看看用户是哪里的。



如果用户 拒绝的位置权限,如何在发起  确认请求。

    
  check_pos() {
    //如何用户 关闭位置,会跳转到 开启的设置界面=wx.openSetting()
    wx.getSetting({
      success(res) {
        const authStatus = res.authSetting['scope.userLocation']
        if (authStatus === false) {
          console.log("重新授权")
          wx.showModal({
            title: '位置权限提示',
            content: '需要您授权位置信息以提供周边服务',
            success(res) {
              if (res.confirm) wx.openSetting()
            }
          })
        }
      }
    })
  },

image.png


image.png






根据 经纬度 换城市 

const QQMapWX = require('./qqmap-wx-jssdk.min.js')
const qqmapsdk = new QQMapWX({
  key: '您的腾讯地图KEY' // 需替换实际key
})

export const getCityByLocation = (latitude, longitude) => {
  return new Promise((resolve, reject) => {
    qqmapsdk.reverseGeocoder({
      location: { latitude, longitude },
      success: res => resolve(res.result.address_component.city),
      fail: err => reject(err)
    })
  })
}



import { getCityByLocation } from '../../utils/location'

Page({
  onLoad() {
    wx.getLocation({
      type: 'gcj02',
      success: async (res) => {
        try {
          const city = await getCityByLocation(res.latitude, res.longitude)
          console.log('当前城市:', city)
        } catch (e) {
          console.error('解析失败', e)
        }
      }
    })
  }
})





手动选择地点chooseLocation 

selectMap() {
var this_ = this;
wx.chooseLocation({
success: function(res) {
console.log(res);
console.log(res.address);
console.log(res.name);
console.log(res.latitude);
console.log(res.longitude);
},
})
}


chooseLocation 需要权限的

  "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息将用于,订单创建"
    }
  },
  "requiredPrivateInfos": ["getLocation","chooseLocation"]


image.png


choose可以选择位置

image.png image.png

点击   定位图标    会重新回到你的当前位置。 上面还可以搜索地点。














openLocation 是打开指定的位置的地图。不用权限直接用

  dao_hang() {
    //根据坐标打开地图
    // 缩放18 好像没有效果  必须要手动放大
    wx.openLocation({
      latitude: Number(this.data.goods.lat),
      longitude: Number(this.data.goods.lng),
      scale: 18,
      name: this.data.goods.name,//小明酒店
      address: this.data.goods.address//地址可以自定义。
    })
  },

image.png





联系站长

站长微信:xiaomao0055

站长QQ:14496453