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

效果图:

微信图片_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

完整代码


// pages/map/map-container/map-container.js
import {
  baipaoGps
} from "../../../utils/api/car.js";
//百度地图
var bmap = require('../../../libs/bmap-wx.min.js');
//高德地图
var amapFile = require('../../../libs/amap-wx.130');
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: [], //点位数据
    gpsInfo: {}, //定位数据
  },

  onLoad: function (options) {
    if (options) {
      if (options.gpsInfo) {
        const decodedGpsInfo = decodeURIComponent(options.gpsInfo);
        const gpsInfo = JSON.parse(decodedGpsInfo);

        if (gpsInfo.type === '2') {
          // // 创建百度地图实例
          const BaiDuMap = new bmap.AMapWX({
            key: '', // 你的高德地图API Key
          });
        } else if (gpsInfo.type === '2') {
          // 创建高德地图实例
          const myAmapFun = new amapFile.AMapWX({
            key: '', // 你的高德地图API Key
          });
        }
        this.data.markers.push(gpsInfo);
        this.setData({
          markers: this.data.markers
        });
        // //初始化地图
        // this.mapCtx = wx.createMapContext('map');
        this.loadMarkers();
      }

    } else {
      wx.showToast({
        title: '车牌号为空',
        icon: 'none'
      })
    }
  },

  loadMarkers: function () {
    //生成 markers 列表,用于在地图上展示
    let markersData = this.data.markers.map(marker => {
      // console.log(marker)
      var a = this.transfor2Mars(Number(marker.lat), Number(marker.lng));
      return {
        id: Number(marker.id),
        longitude: a.Lng,
        latitude: a.Lat,
        vehNum: marker.licenseName,
        location: marker.address,
        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;
}



  • 15
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 28
    评论
H5实现微信小程序地图导航可以通过以下步骤实现: 1. 首先,使用H5开发工具创建一个新的小程序项目。在项目中引入相关的H5地图API(例如高德地图、百度地图等)。 2. 在小程序页面的HTML文件中,创建地图容器元素,可以使用div元素并设置宽度和高度等样式。 3. 在小程序页面的JavaScript文件中,获取地图容器元素,并初始化地图对象。根据选择的地图API,可以调用相应的地图初始化函数,传入地图容器的ID、地图配置等参数。 4. 在地图初始化完成后,可以根据用户的定位信息,通过地图API获取用户的当前位置,并将地图中心设置为用户所在位置。 5. 根据需求,可以在页面中添加导航相关的控件,例如搜索框、当前位置按钮等。通过地图API提供的相关功能,实现地点搜索和点击定位按钮回到当前位置的功能。 6. 当用户输入导航目的地时,根据地图API提供的导航功能,可以绘制导航路线,并在地图上展示出来。可以通过设置起点和终点的经纬度,调用地图API中的导航函数,获取导航路线,并将路线绘制在地图上。 7. 在地图导航绘制完成后,可以根据需要,添加额外的交互功能,例如点击路线中的某一点可以显示该点的详细信息、点击地图上的POI点可以获取相关信息等。 通过以上步骤,就可以在H5中实现微信小程序地图导航功能。需要注意的是,由于H5和小程序的环境差异,因此在实现过程中可能会遇到一些兼容性和调用限制方面的问题,需要根据具体的需求和使用地图API进行调整和解决。
评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咕噜咕噜wy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值