微信小程序 高德地图路线规划实现过程详解

百度地图 百度坐标 (BD-09)

腾讯地图 火星坐标(GCJ-02)

高德地图 火星坐标(GCJ-02)

微信小程序中使用的是腾讯地图作为底图。因此如果使用百度地图时,需要注意坐标的转换。此类坐标的转换函数在网上都有,这里不做过多解释

准备工作:

1、在做小程序 ---- 路线规划之前,需要准备小程序APPID 以及相应使用地图的KEY值。

2、微信小程序API 之 位置 API wx.getLocation(OBJECT)、wx.chooseLocation(OBJECT)、wx.openLocation(OBJECT)的相应用法:https://www.jb51.net/article/166968.htm

各地图平台-------小程序开发的官方文档

1、高德地图: 微信小程序-路线规划,地图导航功能基于高德地图API官方文档 https://lbs.amap.com/api/wx/guide/route/route

2、百度地图: 微信小程序JavaScript API ----- (百度地图路线规划适用于:android / ios / web,故不适用,排除百度地图)

3、腾讯地图: 微信小程序JavaScript SDK 路线规划 --------- https://lbs.qq.com/qqmap_wx_jssdk/method-direction.html

因此使用高德地图和腾讯地图都可以进行路线规划,通过学习官方文档,了解到其实这两个平台的代码思路是一样的,以下以高德地图为例作详细的说明:

微信小程序 高德地图路线规划实现过程详解

高德地图-路线规划开发:根据官方文档demo进行开发 :https://lbs.amap.com/api/wx/guide/route/route

注意:数组数据在setData时候的使用方法

var markesName = "markers[" + 0 + "].name"; that.setData({ [markesName]: name, })

注意需要先加载头部的相关文件

var amapFile = require('../../libs/amap-wx.js'); var config = require('../../libs/config.js');

文件config.js

var config = { key: '1***********************' } module.exports.Config = config;

效果图:

微信小程序 高德地图路线规划实现过程详解

微信小程序 高德地图路线规划实现过程详解

相关代码:

location.js

var amapFile = require('../../libs/amap-wx.js'); var config = require('../../libs/config.js'); const app = getApp() Page({ /** * 页面的初始数据 */ data: { markers: [{ iconPath: "../../img/mapicon_navi_s.png", id: 0, latitude: 39.989643, longitude: 116.481028, width: 23, height: 33 }, { iconPath: "../../img/mapicon_navi_e.png", id: 0, latitude: 39.90816, longitude: 116.434446, width: 24, height: 34 }], distance: '', cost: '', state: 0, polyline: [] }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { console.log(11); var that = this wx.showLoading({ title: "定位中", mask: true }) wx.getLocation({ type: 'gcj02', altitude: true, //高精度定位 success: function(res) { console.info(res); var latitude = res.latitude var longitude = res.longitude var speed = res.speed var accuracy = res.accuracy that.setData({ markers: [{ name: '当前位置', latitude: latitude, longitude: longitude }, { name: '您要去哪儿?', latitude: '', longitude: '' }] }) }, fail: function() { wx.showToast({ title: "定位失败", icon: "none" }) }, complete: function() { wx.hideLoading() } }) }, getFormAddress: function() { var that = this; wx.chooseLocation({ success: function(res) { console.log(res); var name = res.name var address = res.address var latitude = res.latitude var longitude = res.longitude var markesName = "markers[" + 0 + "].name"; var markesLatitude = "markers[" + 0 + "].latitude"; var markeslongitude = "markers[" + 0 + "].longitude"; var markesiconPath = "markers[" + 0 + "].iconPath"; that.setData({ [markesName]: name, [markesLatitude]: latitude, [markeslongitude]: longitude, [markesiconPath]: "../../img/mapicon_navi_s.png" }) console.log('address1', that.data); }, fail: function() { wx.showToast({ title: '定位失败', icon: "none" }) }, complete: function() { //隐藏定位中信息进度 wx.hideLoading() } }) }, getToAddress: function() { var that = this; wx.chooseLocation({ success: function(res) { console.log(res); var name = res.name var address = res.address var latitude = res.latitude var longitude = res.longitude var markesName = "markers[" + 1 + "].name"; var markesLatitude = "markers[" + 1 + "].latitude"; var markeslongitude = "markers[" + 1 + "].longitude"; var markesiconPath = "markers[" + 1 + "].iconPath"; that.setData({ [markesName]: name, [markesLatitude]: latitude, [markeslongitude]: longitude, [markesiconPath]: "../../img/mapicon_navi_e.png" }) console.log('address1', that.data); }, fail: function() { wx.showToast({ title: '定位失败', icon: "none" }) }, complete: function() { //隐藏定位中信息进度 wx.hideLoading() } }) }, /** * 确定 */ getSure: function() { var that = this; var origin = that.data.markers[0].longitude + ',' + that.data.markers[0].latitude; var destination = that.data.markers[1].longitude + ',' + that.data.markers[1].latitude; app.origin = origin; app.destination = destination; console.log('origin', origin); console.log('destination', destination); var key = config.Config.key; var myAmapFun = new amapFile.AMapWX({ key: key }); myAmapFun.getDrivingRoute({ origin: origin, destination: destination, // origin: '116.481028,39.989643', // destination: '116.434446,39.90816', success: function(data) { var points = []; if (data.paths && data.paths[0] && data.paths[0].steps) { var steps = data.paths[0].steps; for (var i = 0; i < steps.length; i++) { var poLen = steps[i].polyline.split(';'); for (var j = 0; j < poLen.length; j++) { points.push({ longitude: parseFloat(poLen[j].split(',')[0]), latitude: parseFloat(poLen[j].split(',')[1]) }) } } } that.setData({ state: 1, polyline: [{ points: points, color: "#0091ff", width: 6 }] }); if (data.paths[0] && data.paths[0].distance) { that.setData({ distance: data.paths[0].distance + '米' }); } if (data.taxi_cost) { that.setData({ cost: '打车约' + parseInt(data.taxi_cost) + '元' }); } console.log('that', that); } }) }, /** * 详情页 */ goDetail: function() { var that = this; wx.navigateTo({ url: '../detail/detail' }) } }) 

location.wxml

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/320b7de6d9d92f5c9cbe520bd5c4204b.html