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

高德地图实现-微信小程序地图导航

效果图:

微信图片_20230806201545.png微信图片_20230806201628.png

一、准备阶段

  • 1、在高德开放平台注册成为开发者
  • 2、申请开发者密钥(key)。
  • 3、下载并解压高德地图微信小程序SDK

高德开放平台:

注册账号(https://lbs.amap.com/)) 申请小程序应用的 key
image.png

应用管理(https://console.amap.com/dev/key/app)) -> 我的应用 -> 创建新应用
image.png
生成的 key 即可用在程序中

  • 下载相关 sdk 文件,导入 amap-wx.js 到项目中:https://lbs.amap.com/api/wx/download

微信小程序:

1.在创建的项目中,新建一个名为 libs 目录,将 amap-wx.js (amap-wx.js 从相关下载页面下载的 zip 文件解压后得到)文件拷贝到 libs 的本地目录下,如下图所示。
image.png

2.设置小程序安全通讯域名,为了保证高德小程序 SDK 中提供的功能的正常使用,需要设置安全域名。
登录微信公众平台,在 “设置”->“开发设置” 中设置 request 合法域名,将 https://restapi.amap.com 中添加进去,如下图所示:

  1. app.json中加入
"permission": {"scope.userLocation": {"desc": "你的位置信息将用于小程序位置接口的效果展示","dowload": "您的文件访问权限用于打开文件"}},

在你需要引用地图的页面的js文件中引入 amap-wx.js 文件。

var amapFile = require('../../../libs/amap-wx.130'); //如:..­/..­/libs/amap-wx.js

完整代码


import {baipaoGps
} from "../../../utils/api/car.js";
var amapFile = require('../../../libs/amap-wx.130'); //如:..­/..­/libs/amap-wx.js
Page({data: {longitude: 117.3590007750093, //经度latitude: 22.846911841310202, //维度scale: 17, //地图默认缩放等级showModal: false, //弹框显隐modalData: {}, //详情信息G: {pi: 3.141592653589793,a: 6378245,ee: .006693421622965943,x_pi: 52.35987755982988},licence:'',//车牌号markers:[],//点位数据},onLoad: function (options) {if (options) {this.setData({licence:options.licence})//初始化地图this.mapCtx = wx.createMapContext('map');this.baipaoGps(options).then((res) => {if (res) {//加载点位数据this.loadMarkers();} })}else{wx.showToast({title:  '车牌号为空',icon: 'none'})}},/*** 请求数据*/baipaoGps(options){return new Promise((resolve, reject) => {console.log(options)baipaoGps(options).then(res => {if (res.data.success) {this.data.markers.push(res.data.result)resolve(res.data.result)} else {wx.showToast({title:  res.data.message,icon: 'none'})}})})},loadMarkers: function () {//生成 markers 列表,用于在地图上展示let markersData = this.data.markers.map(marker => {// console.log(marker)var a = this.transfor2Mars( Number(marker.baipaoGpsInfoBean.gps.lat),  Number(marker.baipaoGpsInfoBean.gps.lng));return {id: Number(marker.extra.id),longitude: a.Lng,latitude: a.Lat,vehNum: marker.extra.licenseName,location:marker.baipaoGpsInfoBean.location,iconPath: '../../../static/img/marker.png',width: 40,height: 40,};});this.setData({markers: markersData,longitude:markersData[0].longitude,latitude:markersData[0].latitude});},// 点击标记点时触发markertap(e) {//点击 marker 时触发,获取对应的点位信息并展示弹框// console.log(e.detail)let markerData = this.data.markers.find(marker => marker.id === e.detail.markerId);this.setData({showModal: true,modalData: markerData});},// 关闭弹框onClose() {this.setData({showModal: false});},navigateToMap() {const modalData = this.data.modalData;const { longitude, latitude } = modalData;// 调用小程序API打开高德地图并进行导航wx.openLocation({longitude,latitude,name: modalData.location, // 标记点名称,可根据实际情况设置scale: 18, // 地图缩放级别,可根据实际情况设置});},isOutOfChina(e, a) {return a < 72.004 || a > 137.8347 || (e < .8293 || e > 55.8271)},transforLat(e, a) {var t = 2 * e - 100 + 3 * a + .2 * a * a + .1 * e * a + .2 * Math.sqrt(Math.abs(e));return t += 2 * (20 * Math.sin(6 * e * this.data.G.pi) + 20 * Math.sin(2 * e * this.data.G.pi)) / 3,t += 2 * (20 * Math.sin(a * this.data.G.pi) + 40 * Math.sin(a / 3 * this.data.G.pi)) / 3,t += 2 * (160 * Math.sin(a / 12 * this.data.G.pi) + 320 * Math.sin(a * this.data.G.pi / 30)) / 3},transforLng(e, a) {var t = 300 + e + 2 * a + .1 * e * e + .1 * e * a + .1 * Math.sqrt(Math.abs(e));return t += 2 * (20 * Math.sin(6 * e * this.data.G.pi) + 20 * Math.sin(2 * e * this.data.G.pi)) / 3,t += 2 * (20 * Math.sin(e * this.data.G.pi) + 40 * Math.sin(e / 3 * this.data.G.pi)) / 3,t += 2 * (150 * Math.sin(e / 12 * this.data.G.pi) + 300 * Math.sin(e / 30 * this.data.G.pi)) / 3},transfor2Mars(e, a) {if (this.isOutOfChina(e, a))return {Lat: e,Lng: a};var t = this.transforLat(a - 105, e - 35),r = this.transforLng(a - 105, e - 35),n = e / 180 * this.data.G.pi,o = Math.sin(n);o = 1 - this.data.G.ee * o * o;var s = Math.sqrt(o);return {Lat: e + (t = 180 * t / (this.data.G.a * (1 - this.data.G.ee) / (o * s) * this.data.G.pi)),Lng: a + (r = 180 * r / (this.data.G.a / s * Math.cos(n) * this.data.G.pi))}},
})
<view><!-- 地图控件 --><view><map id="map" longitude="{{longitude}}" latitude="{{latitude}}" scale="{{scale}}" markers="{{markers}}"bindmarkertap="markertap"></map></view><!-- 弹框 --><view><van-popup closeable bind:close="onClose" round custom-style="height: 30%" position="bottom" show="{{ showModal }}"bind:close="onClose"><view class="detailsBox"><view><text>车牌号码 :</text><text>{{modalData.vehNum}}</text></view><view><text>所在位置:</text><text>{{modalData.location}}</text><view class="dh" bindtap="navigateToMap">地图导航</view><image style="width:30px;height:30px;margin-top: 5px;position: absolute;"src="/static/img/map/daohang.png"></image></view></view></van-popup></view>
</view>
#map{width: 100%;height: 100vh;
}.detailsBox{padding: 20rpx 20rpx 0rpx 28rpx;font-size: 28rpx;
}
.detailsBox view:nth-child(n+2){margin-top: 20rpx;
}
view{font-size: 18px;
}
.dh {display: inline-block;/* padding: 10px 20px; */color: rgb(76, 36, 255);font-size: 15px;border-radius: 4px;cursor: pointer;transition: background-color 0.3s ease;margin-left: 20px;
}.dh:hover {background-color: #0056b3;
}
http://www.lryc.cn/news/168036.html

相关文章:

  • 你已经应用了哪种服务注册和发现的模式呢?
  • Python爬虫技术在SEO优化中的关键应用和最佳实践
  • 基于支持向量机的试剂条图像识别,基于SVM的图像识别,SVM的详细原理,Libsvm工具箱使用注意事项
  • PbootCMS在搭建网站
  • C语言经典100例题(56-60)--画圆;画方;画线
  • linux相关知识以及有关指令3
  • 关于Synchronized
  • 深度学习(Python)学习笔记2
  • gitlab操作
  • docker day04
  • 任意区域的色彩一致性处理方法
  • 听GPT 讲Istio源代码--pilot
  • pdfjs解决ie浏览器预览pdf问题
  • 千里共婵娟 | 结合微信公众号用JavaScript完整开发实现换中秋头像的功能
  • pt26django教程
  • javabasic
  • 电子词典项目
  • SQL12 高级操作符练习(2)
  • python写代码过程中的坑230915
  • SpringBoot课堂笔记20230913
  • 3D数字孪生:从3D数据采集到3D内容分析
  • 【杂记】git管理工具的相关应用
  • 看好多人都在劝退学计算机,可是张雪峰又 推荐过计算机,所以计算机到底是什么样 的?
  • leetcode 26.删除有序数组中的重复项
  • jitsi音视频会议集成到自己的网页中
  • 浅析Java责任链模式实现
  • SpringBoot雪花算法主键ID传到前端后精度丢失问题的解决
  • vue使用elementUI的upload上传文件封装
  • cenos自动启动tomcat
  • OSI模型与数据的封装