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

vue3+uniapp中使用高德地图实现撒点效果

前言:

        vue3+uniapp中使用高德地图实现撒点效果

实现效果:

操作步骤:

1、引入高德插件,并生成js配置插件,详情步骤请点我

import amapFile from '../../libs/amap-wx.js'

2、页面上配置我们的map标签

<template><!-- 地图控件 --><view><map id="map" :longitude="mapObj.longitude" :latitude="mapObj.latitude" :scale="mapObj.scale" :markers="mapObj.markers"@markertap="markertap"@click="mapClick"></map></view>
</template>

3、js部分,定义我们相关变量

let mapObj = reactive({longitude:116.481028, //经度latitude:39.989643, //维度scale:17, //地图默认缩放等级markers: [], //点位数据})let locationListener = ref('')let initMap = function(){const myAmapFun = new amapFile.AMapWX({key: 'bb****', // 你的高德地图API Key});console.log('myAmapFun',myAmapFun)}onShow(()=>{initMap()initMapWZ()})

4、获取我们当前位置

// 获取当前位置信息let initMapWZ = function(){console.log('init')// uni.getLocation uniapp官网提供的获取定位的方法,调用过多会导致无法使用,需要使用监听方法uni.getLocation({type: 'gcj02', //国测局坐标 gcj02,要使用地图map必须使用这个success: res=> {getNowDWBackFun(res)},fail:err=>{//getLocation:fail 频繁调用会增加电量损耗,可考虑使用 wx.onLocationChange 监听地理位置变化console.log(err)startLocationWatch()},complete:()=>{console.log('complete')}})}

5、更新我们当前实时位置

let startLocationWatch = ()=> {// 1. 检查权限uni.authorize({scope: 'scope.userLocation',success: () => {// 2. 开启位置更新uni.startLocationUpdate({success: () => {// 3. 监听位置变化locationListener = uni.onLocationChange((res) => {// 在此更新地图或处理位置数据getNowDWBackFun(res)})},fail: (err) => {console.error('启动位置更新失败:', err)}})},fail: () => {uni.showModal({title: '权限提示',content: '需要位置权限以持续获取位置',success: (res) => {if (res.confirm) uni.openSetting()}})}})}

6、将我们当前位置,用图片展示在地图上

// 拿到当前最新位置以后的回调方法let getNowDWBackFun = res=>{console.log('当前位置的经度:' + res.longitude);console.log('当前位置的纬度:' + res.latitude);mapObj.longitude = res.longitudemapObj.latitude = res.latitudemapObj.markers = [{id: 1,longitude:res.longitude,latitude: res.latitude,iconPath: '../../static/now.png',title: '当前位置',width:25,height:25}]}

7、当我们的界面关闭时候,停止我们的实时更新位置方法

// 停止监听let stopLocationWatch = ()=>{if (locationListener.value) {uni.stopLocationUpdate() // 停止位置更新}}onHide(()=>{stopLocationWatch()})

相关文章:

  • Linux中的文件介绍
  • C++ 常见知识积累
  • Nginx 强制 HTTPS:提升网站安全性的关键一步
  • Temporary failure in name resolution
  • DVWA-XSS
  • PT5F2307触摸A/D型8-Bit MCU
  • 【Flutter】创建BMI计算器应用并添加依赖和打包
  • Flutter 中 build 方法为何写在 StatefulWidget 的 State 类中
  • 【Vue 3 步骤进度条组件实现与使用教程】
  • RESTful API设计:从原则到Gin实现
  • Rust 学习笔记:泛型
  • 从电商角度设计大模型的 Prompt
  • Baklib知识中台驱动智能服务创新
  • 牛客网NC15869:长方体边长和计算问题解析
  • 力扣热题100, 力扣.167两数之和II 力扣80.删除有序数组中的重复项力扣99.恢复二叉搜索树力扣.110平衡二叉树
  • AtCoder 第406场初级竞赛 A~E题解
  • 如何在element ui中el-select的选择项目中添加自定义图标
  • ABC 353
  • 建立java项目
  • 【电商接口】如何通过小红书订单API实现高效电商数据管理?
  • 鸿蒙最新版本/有没有免费的seo网站
  • asp.net做网站怎么样/百度排行榜风云