微信小程序通过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"
]
相关推荐
是一碗螺丝粉13 小时前
拯救你的app/小程序审核!一套完美避开审核封禁的URL黑名单机制
前端·javascript·微信小程序
weixin_lynhgworld17 小时前
盲盒抽谷机小程序系统开发:从0到1的完整方法论
小程序
weixin_lynhgworld17 小时前
短剧小程序系统开发:赋能创作者,推动短剧艺术创新发展
小程序
一匹电信狗20 小时前
【C++】异常详解(万字解读)
服务器·c++·算法·leetcode·小程序·stl·visual studio
我叫黑大帅21 小时前
微信小程序分包:告别加载慢,像拆快递一样简单!
前端·微信小程序
两个月菜鸟1 天前
vue+微信小程序 五角星
前端·vue.js·微信小程序
说私域1 天前
基于开源AI智能客服、AI智能名片与S2B2C商城小程序的微商服务优化及复购转介绍提升策略研究
人工智能·小程序
熬耶2 天前
Uniapp之微信小程序自定义底部导航栏形态
微信小程序·小程序·uni-app
IT毕设实战小研2 天前
基于Spring Boot校园二手交易平台系统设计与实现 二手交易系统 交易平台小程序
java·数据库·vue.js·spring boot·后端·小程序·课程设计
weixin_177297220692 天前
剧本杀小程序系统开发:重构推理娱乐生态
小程序·重构·娱乐