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


Posted in Javascript onAugust 05, 2019

前言

最近项目中做到相关网约车小程序。需要使用到地图中的路线规划,对3种地图进行了分析。这里稍微做一下总结:

  • 百度地图 百度坐标 (BD-09)
  • 腾讯地图 火星坐标(GCJ-02)
  • 高德地图 火星坐标(GCJ-02)

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

准备工作:

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

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

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

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

2、百度地图: 微信小程序JavaScript API ----- http://lbsyun.baidu.com/index.php?title=wxjsapi (百度地图路线规划适用于: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

<view class="map_title">
 <view bindtap='getFormAddress'>
 出发地:<input placeholder="出发地" type="text" name="" bindinput="" value='{{markers[0].name}}' />
 </view>
 <view bindtap='getToAddress'>
 目的地:<input placeholder="目的地" type="text" name="" bindinput="" value='{{markers[1].name}}' />
 </view>
 <button bindtap = 'getSure'>确定</button>
</view>
<view wx:if="{{state==1}}">
 <view class="map_box">
 <map id="navi_map" longitude="{{markers[0].longitude}}" latitude="{{markers[0].latitude}}" scale="12" markers="{{markers}}" polyline="{{polyline}}"></map>
 </view>
 <view class="text_box">
 <view class="text">{{distance}}</view>
 <view class="text">{{cost}}</view>
 <view class="detail_button" bindtouchstart="goDetail">详情</view>
 </view>
</view>

location.wxss

.flex-style{
 display: -webkit-box;
 display: -webkit-flex;
 display: flex;
}
.flex-item{
 height: 35px;
 line-height: 35px;
 text-align: center;
 -webkit-box-flex: 1;
 -webkit-flex: 1;
 flex: 1
}
.flex-item.active{
 color:#0091ff;
}
.map_title{
 position:absolute;
 top: 10px;
 bottom: 110px;
 left: 0px;
 right: 0px;
}
.map_btn{
 position:absolute;
 top: 120px;
 bottom: 220px;
 left: 0px;
 right: 0px;
}
.map_box{
 position:absolute;
 top: 160px;
 bottom: 90px;
 left: 0px;
 right: 0px;
}
#navi_map{
 width: 100%;
 height: 100%;
}
.text_box{
 position:absolute;
 height: 90px;
 bottom: 0px;
 left: 0px;
 right: 0px;
}
.text_box .text{
 margin: 15px;
}
.detail_button{
 position:absolute;
 bottom: 30px;
 right: 10px;
 padding: 3px 5px;
 color: #fff;
 background: #0091ff;
 width:50px;
 text-align:center;
 border-radius:5px;
}

点击详情跳转页,显示导航详细说明:

detail.js

var amapFile = require('../../libs/amap-wx.js');
var config = require('../../libs/config.js');
const app = getApp()
Page({
 data: {
 steps: {}
 },
 onLoad: function () {
 var that = this;
 var key = config.Config.key;
 var myAmapFun = new amapFile.AMapWX({ key: key });
 myAmapFun.getDrivingRoute({
  origin: app.origin,
  destination: app.destination,
  success: function (data) {
  if (data.paths && data.paths[0] && data.paths[0].steps) {
   that.setData({
   steps: data.paths[0].steps
   });
  }
  },
  fail: function (info) {
  }
 })
 }
})

detail.wxml

<view class="text_box" wx:for="{{steps}}" wx:for-item="i" wx:key="j">
 {{i.instruction}}
</view>

这只是个人的一个demo用例。仅做参考。其中还有很多瑕疵,不要介意哈。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
jquery 多行滚动代码(附详细解释)
Jun 17 Javascript
js隐藏与显示回到顶部按钮及window.onscroll事件应用
Jan 25 Javascript
js实现仿百度瀑布流的方法
Feb 05 Javascript
JavaScript判断用户是否对表单进行了修改的方法
Mar 18 Javascript
理解JS绑定事件
Jan 19 Javascript
详解JS面向对象编程
Jan 24 Javascript
javascript 数组去重复(在线去重工具)
Dec 17 Javascript
Bootstrap modal 多弹窗之叠加关闭阴影遮罩问题的解决方法
Feb 27 Javascript
JS实现生成由字母与数字组合的随机字符串功能详解
May 25 Javascript
vue路由跳转传参数的方法
May 06 Javascript
深入理解令牌认证机制(token)
Aug 22 Javascript
vue中父子组件的参数传递和应用示例
Jan 04 Vue.js
详解将微信小程序接口Promise化并使用async函数
Aug 05 #Javascript
使用apifm-wxapi模块中的问题及解决方法
Aug 05 #Javascript
使用apifm-wxapi快速开发小程序过程详解
Aug 05 #Javascript
浅谈Vue项目骨架屏注入实践
Aug 05 #Javascript
基于 vue-skeleton-webpack-plugin 的骨架屏实战
Aug 05 #Javascript
JS 自执行函数原理及用法
Aug 05 #Javascript
jQuery提示框插件SweetAlert用法分析
Aug 05 #jQuery
You might like
全国FM电台频率大全 - 6 辽宁省
2020/03/11 无线电
编写PHP的安全策略
2006/10/09 PHP
php chr() ord()中文截取乱码问题解决方法
2008/09/08 PHP
解析php dirname()与__FILE__常量的应用
2013/06/24 PHP
file_get_contents(&quot;php://input&quot;, &quot;r&quot;)实例介绍
2013/07/01 PHP
PHP IE中下载附件问题解决方法
2014/01/07 PHP
PHP随机生成随机个数的字母组合示例
2014/01/14 PHP
一个简洁实用的PHP缓存类完整实例
2014/07/26 PHP
smarty自定义函数htmlcheckboxes用法实例
2015/01/22 PHP
Zend Framework框架之Zend_Mail实现发送Email邮件验证功能及解决标题乱码的方法
2016/03/21 PHP
JS弹出层的显示与隐藏示例代码
2013/12/27 Javascript
jquery中map函数与each函数的区别实例介绍
2014/06/23 Javascript
jQuery中$.ajax()和$.getJson()同步处理详解
2015/08/12 Javascript
js实现超简单的展开、折叠目录代码
2015/08/28 Javascript
详解Node.js包的工程目录与NPM包管理器的使用
2016/02/16 Javascript
微信小程序实现图片自适应(支持多图)
2017/01/25 Javascript
js放到head中失效的原因与解决方法
2017/03/07 Javascript
react实现pure render时bind(this)隐患需注意!
2017/03/09 Javascript
JS设计模式之单例模式(一)
2017/09/29 Javascript
JS动画定时器知识总结
2018/03/23 Javascript
js运算符的一些特殊用法
2018/07/29 Javascript
在layer弹层layer.prompt中,修改placeholder的实现方法
2019/09/27 Javascript
python实现通过shelve修改对象实例
2014/09/26 Python
python实现requests发送/上传多个文件的示例
2018/06/04 Python
浅谈python3发送post请求参数为空的情况
2018/12/28 Python
使用Python实现分别输出每个数组
2019/12/06 Python
linux 下python多线程递归复制文件夹及文件夹中的文件
2020/01/02 Python
Pytorch在NLP中的简单应用详解
2020/01/08 Python
专业求职信撰写要诀
2014/02/18 职场文书
农业局学习党的群众路线教育实践活动心得体会
2014/03/07 职场文书
跟单业务员岗位职责
2014/03/08 职场文书
企业演讲稿范文大全
2014/05/20 职场文书
小学运动会开幕词
2015/01/28 职场文书
体育教师个人总结
2015/02/09 职场文书
追悼词范文大全
2015/06/23 职场文书
原生Js 实现的简单无缝滚动轮播图的示例代码
2021/05/10 Javascript