微信小程序通过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"
]

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值