微信小程序-路线规划功能

打印 上一主题 下一主题

主题 947|帖子 947|积分 2841

wxml
  1. <view class="container">
  2.   <view class="mapTop">
  3.     <van-tabs active="{{ active }}" color="#2A82E4" bind:change="onChange">
  4.       <view wx:for="{{scenicList}}" wx:key="id">
  5.         <van-tab title="{{item.name}}" />
  6.       </view>
  7.     </van-tabs>
  8.     <!-- 显示地图 -->
  9.     <view class="view" style="position: absolute;width: 100%;height: 80%;bottom: 20%">
  10.       <map id="myMap"
  11.            style="width: 100%; height: 100vh;"
  12.            longitude="{{longitude}}"
  13.            latitude="{{latitude}}"
  14.            scale="16"
  15.            polyline="{{polyline}}"
  16.            show-location="true"
  17.            markers="{{markers}}">
  18.       </map>
  19.     </view>
  20.   </view>
  21. </view>
复制代码
wxss
  1. /* pages/atlas/index.wxss */
  2. .container {
  3.     background-color: #ffffff;
  4.     padding-bottom: 10px;
  5. }
  6. /* 顶部导航栏 */
  7. .top {
  8.     width: 100%;
  9.     height: 90px;
  10.     color: #636363;
  11.     display: flex;
  12.     top: 0;
  13.     position: fixed;
  14.     font-size: 14px;
  15.     text-align: center;
  16.     align-items: center;
  17.     z-index: 5;
  18.     background-color: #ffffff;
  19.     border-bottom: 1px #e1e1e1 solid;
  20. }
  21. .top text {
  22.     position: absolute;
  23.     bottom: 10px;
  24. }
  25. .left {
  26.     width: 65px;
  27.     height: 25px;
  28.     margin-left: 10px;
  29.     display: flex;
  30.     justify-content: space-around;
  31.     align-items: center;
  32.     border: 1px #d4d4d4 solid;
  33.     border-radius: 20px;
  34.     position: absolute;
  35.     bottom: 10px;
  36. }
  37. .left image {
  38.     width: 15px;
  39.     height: 15px;
  40. }
  41. .return {
  42.     transform: rotate(180deg);
  43. }
  44. .line {
  45.     width: 1px;
  46.     height: 60%;
  47.     background-color: #d7c3ac;
  48. }
  49. #house {
  50.     width: 20px;
  51.     height: 20px;
  52. }
  53. .mapTop {
  54.     width: 100%;
  55. }
  56. .mapTop-block {
  57.     width: 15%;
  58.     height: 100%;
  59.     display: flex;
  60.     flex-direction: column;
  61.     align-items: center;
  62.     justify-content: center;
  63.     font-size: 0.9rem;
  64.     background-color: brown;
  65. }
  66. .mapTop-line {
  67.     width: 80%;
  68.     height: 2px;
  69.     margin-top: 5px;
  70.     background-color: chartreuse;
  71. }
复制代码
js
  1. var QQMapWX = require('./qqmap-wx-jssdk');
  2. // 实例化API核心类
  3. var qqmapsdk = new QQMapWX({
  4.     key: 'HLBBZ-UXNCV-OUOPC-5AT7V-Y3UG6-UYB5U' // 必填
  5. });
  6. Page({
  7.     // 返回上一页
  8.     returnClick() {
  9.         wx.navigateBack();
  10.     },
  11.     targetHouse() {
  12.         wx.switchTab({
  13.             url: '/pages/goods-list/index',
  14.         });
  15.     },
  16.     data: {
  17.         srcLat: '35.280000',  // 起点纬度(字符串)
  18.         srcLng: '113.980000',  // 起点经度(字符串)
  19.         strategyArrlat: '34.52',  // 终点纬度(字符串)
  20.         strategyArrlng: '110.07', // 终点经度(字符串)
  21.         polyline: [], // 用于存储路线的多段路径
  22.         markers: [] // 用于存储起点和终点的标记
  23.     },
  24.     onLoad() {
  25.         var _this = this;
  26.         // 将坐标转换为数字类型
  27.         var srcLat = parseFloat(_this.data.srcLat);
  28.         var srcLng = parseFloat(_this.data.srcLng);
  29.         var strategyArrlat = parseFloat(_this.data.strategyArrlat);
  30.         var strategyArrlng = parseFloat(_this.data.strategyArrlng);
  31.         // 请求页面数据
  32.         wx.login({
  33.             success: (res) => {
  34.                 if (res.code) {
  35.                     // 请求自己的后台服务器,传递 code 获取 session_key 和 openid
  36.                     // 请求景区分类列表
  37.                     wx.request({
  38.                         url: 'https://jingqu.kuxia.top/app/maps/index', // 替换为你自己的服务器地址
  39.                         method: 'POST',
  40.                         data: {
  41.                             scenic_id: 3
  42.                         },
  43.                         success: (response) => {
  44.                             if (response.data.code == 1) {
  45.                                 console.log('景区分类列表', response.data.data);
  46.                                 this.setData({
  47.                                     scenicList: response.data.data // 更新数据
  48.                                 });
  49.                             }
  50.                         },
  51.                         fail: (err) => {
  52.                             console.error('请求失败', err);
  53.                         }
  54.                     });
  55.                 }
  56.             }
  57.         });
  58.         // 调用距离计算接口
  59.         qqmapsdk.direction({
  60.             mode: 'driving',
  61.             from: {
  62.                 latitude: srcLat,
  63.                 longitude: srcLng
  64.             },
  65.             to: {
  66.                 latitude: strategyArrlat,
  67.                 longitude: strategyArrlng
  68.             },
  69.             success: function (res) {
  70.                 var ret = res;
  71.                 var coors = ret.result.routes[0].polyline,
  72.                     pl = [];
  73.                 var kr = 1000000;
  74.                 for (var i = 2; i < coors.length; i++) {
  75.                     coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
  76.                 }
  77.                 for (var i = 0; i < coors.length; i += 2) {
  78.                     pl.push({
  79.                         latitude: coors[i],
  80.                         longitude: coors[i + 1]
  81.                     });
  82.                 }
  83.                 // 设置polyline属性,将路线显示出来
  84.                 _this.setData({
  85.                     latitude: pl[0].latitude,
  86.                     longitude: pl[0].longitude,
  87.                     polyline: [{
  88.                         points: pl,
  89.                         color: '#2A82E4',
  90.                         width: 5
  91.                     }],
  92.                     markers: [{
  93.                         id: 1,
  94.                         latitude: srcLat,
  95.                         longitude: srcLng,
  96.                         iconPath: '/images/start.png', // 起点图标路径
  97.                         width: 30,
  98.                         height: 30
  99.                     }, {
  100.                         id: 2,
  101.                         latitude: strategyArrlat,
  102.                         longitude: strategyArrlng,
  103.                         iconPath: '/images/end.png', // 终点图标路径
  104.                         width: 30,
  105.                         height: 30
  106.                     }]
  107.                 });
  108.             },
  109.             fail: function (error) {
  110.                 console.error(error);
  111.             }
  112.         });
  113.     }
  114. });
复制代码
引入文件

 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

勿忘初心做自己

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表