微信小程序通过startLocationUpdate,onLocationChange获取当前地理位置信息,配合腾讯地图解析获取到地址

先创建个getLocation.js文件

复制代码
//获取用户当前所在的位置
const getLocation = () => {
    return new Promise((resolve, reject) => {
        let _locationChangeFn = (res) => {
            resolve(res) // 回传地里位置信息
            wx.offLocationChange(_locationChangeFn) // 关闭实时定位
            wx.stopLocationUpdate(_locationChangeFn); // 关闭监听 不关闭监听的话,有时获取位置时会非常慢
        }
        wx.startLocationUpdate({
            success: (res) => {
                wx.onLocationChange(_locationChangeFn)
            },
            fail: (err) => {
                // 重新获取位置权限
                wx.openSetting({
                    success(res) {
                        res.authSetting = {
                            "scope.userLocation": true
                        }
                    }
                })
                reject(err)
            }
        })
    })
}

module.exports = {
    getLocation
}

在App.vue文件里引入封装的getLocation.js文件

复制代码
<script>
import getLocation from "./pages/common/getLocation.js";
import { addUserTrail } from "@/api/promote.js";
var QQMapWX = require("./common/qqmap-wx-jssdk.js");//引入腾讯地图逆解析地址
var qqmapsdk;
export default {
  data() {
    return {
        latitude:'',
        longitude:''
    };
  },
  onLaunch: function () {
    setInterval(function () {//定时任务判断登录的角色在调用getLocation.js
      if (uni.getStorageSync("ROLE_NAME") !== null &&
        uni.getStorageSync("ROLE_NAME") !== "" &&
        uni.getStorageSync("ROLE_NAME") === "BD") {
        getLocation.getLocation().then((res) => {
          console.log('当前所在位置的经纬度为:')
          console.log(res.latitude,res.longitude)
          const addUserTrailBody = {
            latitude: null,
            longitude: null,
            address: "",
          };
          addUserTrailBody.latitude = res.latitude;
          addUserTrailBody.longitude = res.longitude;
          addUserTrail(addUserTrailBody);
        });
      }
    }, 90000);
  },

  methods: {
    getMapAddress() {
      const that = this;
      const tMap = new QQMapWX({
        key: "xxx", //腾讯地图开发者密钥key
      });
      uni.getLocation({
        type: "wgs84",
        isHighAccuracy: true,
        success: (res) => {
          console.log(res);
        },
        fail: () => {
          console.log("获取经纬度失败");
        },
          complete: () => {
          tMap.reverseGeocoder({//逆解析
            location: {
              latitude: that.latitude,
              longitude: that.longitude,
            },
            success: function (res) {
              console.log("解析地址成功", res);
              console.log("当前地址:", res.result.address);
              const addUserTrailBody = {//拿到地理位置传递下
                latitude: null,
                longitude: null,
                address: "",
              };
              addUserTrailBody.latitude = res.latitude;
              addUserTrailBody.longitude = res.longitude;
              addUserTrailBody.address = res.address;
              addUserTrail(addUserTrailBody);
              uni.setStorage({
                key: "local",
                data: res.result.address,
                success() {
                  console.log("用户地址信息已缓存");
                },
              });
            },
            fail: function (res) {
              uni.showToast({
                title: "定位失败",
                duration: 2000,
                icon: "none",
              });
              console.log(res);
            },
            complete: function (res) {
              //无论成功失败都会执行
              console.log("获取定位信息");
              return;
              uni.openLocation({
                latitude: that.latitude,
                longitude: that.longitude,
                success: function () {
                  console.log("success");
                },
              });
            },
          });
        },
      });
    },    
  },
};
</script>

要在manifest.json文件里配置下内容

复制代码
"permission": {
    "scope.userLocation": {
    "desc": "你的位置信息将用于定位效果展示"
    }
},
"requiredPrivateInfos": [
   "getLocation",
    "onLocationChange",
    "startLocationUpdate",
    "chooseLocation"
],
    "requiredBackgroundModes": [
        "location"
]
相关推荐
2501_915918411 小时前
iOS性能测试工具 Instruments、Keymob的使用方法 不局限 FPS
android·ios·小程序·https·uni-app·iphone·webview
Chengbei111 小时前
利用 LibreNMS snmpget 配置篡改实现 RCE 的完整攻击链
人工智能·web安全·网络安全·小程序·系统安全
苏灵凯7 小时前
智能环境监测终端全栈设计:从单片机到微信小程序,手把手搞定!
单片机·嵌入式硬件·mcu·物联网·微信小程序·小程序·蓝牙模块
nhc0888 小时前
贵阳纳海川科技有限公司・货运物流行业解决方案
科技·微信小程序·小程序·软件开发·小程序开发
admin and root8 小时前
AWS S3 对象存储攻防&云安全之OSS存储桶漏洞
微信小程序·小程序·渗透测试·云计算·aws·src·攻防演练
取码网8 小时前
新版点微同城主题源码34.7+全套插件+小程序前后端 源文件
小程序
2501_915918418 小时前
iOS 混淆流程 提升 IPA 分析难度 实现 IPA 深度加固
android·ios·小程序·https·uni-app·iphone·webview
布吉岛没有岛_10 小时前
小程序接入智能体
小程序·智能体
Soujer10 小时前
支持微信4.0的小程序注入调试工具(WMPFDebugger)
微信·小程序
2501_9159090610 小时前
React Native 上架 App Store:项目运行与审核构建的流程
android·ios·小程序·https·uni-app·iphone·webview