获取位置信息

##获取位置信息(经纬度) 做打卡的时候,用 uni.location 获取打卡的维度:res.latitude , 经度 res.longitude ,他需要用 HbuildX,在 manifest.json 种配置启用 https 协议和定位地图(只能选择一个且需要配置地图的 key,安全密钥,代理地址(www.amap.com/))有的只用配置密钥。

###1.导入 sdk

  1. 导入 sdk 文件以腾讯地图为例包需要下载到本地 这里可以查看地址
import QQMapWX from '@/pages/sys/msg/check/addcheckadress/qqmap-wx-jssdk.js'
  1. 调用 QQMapWX 地图 sdk
      const tMap = new QQMapWX({
        key: // 开发者密钥,
      })
     tMap.reverseGeocoder({
            location: {
              lat: res.latitude,
              lng: res.longitude,
              remark: res.remark,
              name: res.name,
            },
            success: (res) => {
              console.log('解析地址成功', res)
              console.log('当前地址:', res.result.remark)
              // 可以在这里将选定的地址信息保存到缓存或进行其他操作
            },
            fail: (res) => {
              uni.showToast({
                // title: '解析地址失败',
                title: '地址添加成功',
                duration: 2000,
                icon: 'none',
              })
              // console.log(2,res);
            },
          })

###2.用 HbuildX 的内置 sdk

  1. uni.location() 获取打卡的维度:res.latitude , 经度 res.longitude 可参考官方文档 uni.getLocation
uni.getLocation({
  type: 'gcj02', //国内的话使用type: 'gcj02',全国的话使用wgs84坐标,百度用的是bd09
  highAccuracy: true,
  highAccuracyExpireTime: 3000,
  success: function (res) {
    console.log('当前位置的经度:' + res.longitude)
    console.log('当前位置的纬度:' + res.latitude)
  },
})

2.uni.chooseLocation()打开地图选择位置。

uni.chooseLocation({
  success: function (res) {
    console.log('位置名称:' + res.name)
    console.log('详细地址:' + res.address)
    console.log('纬度:' + res.latitude)
    console.log('经度:' + res.longitude)
  },
})

本文章使用limfx的vscode插件快速发布