当前位置: 首页 > news >正文

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

http://www.lryc.cn/news/207516.html

相关文章:

  • C/C++字符三角形 2020年12月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析
  • Python数据挖掘:入门、进阶与实用案例分析——基于非侵入式负荷检测与分解的电力数据挖掘
  • 基于 Qt控制开发板 LED和C语言控制LED渐变亮度效果
  • Android 11.0 禁用插入耳机时弹出的保护听力对话框
  • 微信小程序案例2-3:婚礼邀请函
  • K8S部署Dashboard
  • 【OJ比赛日历】快周末了,不来一场比赛吗? #10.29-11.04 #7场
  • 常用应用安装教程---在centos7系统上安装Docker
  • CTFHub-SSRF-读取伪协议
  • 推荐一款适合科技行业的CRM系统
  • ChatGPT 与 Python Echarts 完成热力图实例
  • vue3项目报错The template root requires exactly one element.eslint-plugin-vue
  • 【C++系列】STL容器——vector类的例题应用(12)
  • 常用应用安装教程---在centos7系统上安装JDK8
  • 阿里云/腾讯云国际站代理:国际腾讯云的优势
  • 【软件教程】如何用C++检查TCP或UDP端口是否被占用
  • Flutter报错RenderBox was not laid out: RenderRepaintBoundary的解决方法
  • 0基础学习PyFlink——用户自定义函数之UDAF
  • MVC架构_Qt自己的MV架构
  • CentOS - 安装 Elasticsearch
  • IDEA 断点高阶
  • Qt中的单例模式
  • ROS自学笔记十五:URDF工具
  • Pytorch代码入门学习之分类任务(三):定义损失函数与优化器
  • 【Linux】安装VMWare虚拟机(安装配置)和配置Windows Server 2012 R2(安装配置连接vm虚拟机)以及环境配置
  • Python入口顶部人体检测统计进出人数
  • 移动端自动化-Appium元素定位
  • menuconfig 图形化配置原理说明三
  • Ansible简介
  • Tomcat+nginx负载均衡和动静分离