小程序 地图起点终点 中间动态

本文介绍了一种在微信小程序中使用腾讯地图API实现路径绘制的方法,并通过不断更新工程师坐标来实时显示移动路线。文章详细解释了如何获取并解析路径坐标,以及如何将这些坐标转换为地图上的可视路径。
// pages/WLXiangqing/WLXiangqing.js
var coors;
import { request } from './../../request/index.js';
Page({
  data: {
    polyline: [],
    markers: [],
  },
  onReady: function () {
    this.mapContext = wx.createMapContext("map", this);
  },
  onLoad: function (options) {
    this.getLatLng();
    // 获取当前地图,设置经纬度,传递过来的坐标,用户下单的坐标地址。
    console.log(options);
    wx.getLocation({
      success: (res) => {
        // this.setData({
        //   latitude: options.latitude,
        //   longitude: options.longitude
        // });
        this.getCenterLocation(res);
        wx.request({
          url: 'https://apis.map.qq.com/ws/direction/v1/driving/?from=' + this.data.markers[0].latitude + ',' + this.data.markers[0].longitude + '&to=' + this.data.markers[1].latitude + ',' + this.data.markers[1].longitude + '&output=json&callback=cb&key=PD5BZ-K2VRO-CPEWZ-SOBAC-4KCDT-KAFLF',
          success: (res) => {
           
            coors = res.data.result.routes[0].polyline
            console.log(coors)
            for (var i = 2; i < coors.length; i++) {
              coors[i] = coors[i - 2] + coors[i] / 1000000
            }
            // console.log(coors)
            //划线
            var b = [];
            for (var i = 0; i < coors.length; i = i + 2) {
              b[i / 2] = {
                latitude: coors[i],
                longitude: coors[i + 1]
              };
              console.log(b[i / 2])
            }
            this.setData({
              polyline: [{
                points: b,
                color: "#00ae20",
                width: 4,
                dottedLine: false
              }],
            })
          }
        })
      },
    });
  },
  //  两个坐标 一个下单地址,一个工程师接单地址,然后不停的更新工程师的坐标位置。
  getCenterLocation: function (res) {
    // request({
    //   url: '/GetDriverAddress',
    //   method: 'POST',
    //   data: {
    //     orderId:wx.getStorageSync('ddbh'),
    //     customerId: wx.getStorageSync('userCode'),
    //     outboundorderId: wx.getStorageSync('outboundorderId')

    //     // outboundorderId: 'WZ00000001',
    //     // orderId: 'XSAA0000590677',
    //     // customerId: '7'

    //   }
    // }).then(res1=>{
    //   console.log(res1)
    //   let markersMap = res.data.Source[0];
    //   this.setData({
    //     markers: [
    //       {
    //         iconPath: "/images/map.png",
    //         id: 0,
    //         latitude: markersMap.stalat,
    //         longitude: markersMap.stalng,
    //         width: 30,
    //         height: 30,
    //         alpha: 0.8,
    //         callout: {
    //           content: " 起始位置 ",
    //           color: "#ffffff",
    //           fontSize: 10,
    //           borderRadius: 10,
    //           bgColor: "#6e707c",
    //           padding: 5,
    //           display: "ALWAYS"
    //         }
    //       },
    //       {
    //         iconPath: "/images/map.png",
    //         id: 1,
    //         latitude: markersMap.endlat,
    //         longitude: markersMap.endlng,
    //         width: 20,
    //         height: 30,
    //         alpha: 0.8,
    //         callout: {
    //           content: " 终点 ",
    //           color: "#ffffff",
    //           fontSize: 10,
    //           borderRadius: 10,
    //           bgColor: "#6e707c",
    //           padding: 5,
    //           display: "ALWAYS"
    //         }
    //       }
    //     ],
    //   })
    // })


    // this.setData({
    //   markers: [{
    //     iconPath: "/images/cart.png",
    //     id: 0,
    //     latitude: res.latitude,
    //     longitude: res.longitude,
    //     width: 30,
    //     height: 30,
    //     alpha: 0.8,
    //     callout: {
    //       content: " 我的位置 ",
    //       color: "#ffffff",
    //       fontSize: 10,
    //       borderRadius: 10,
    //       bgColor: "#6e707c",
    //       padding: 5,
    //       display: "ALWAYS"
    //     }
    //   },
    //   {
    //     iconPath: "/images/map.png",
    //     id: 1,
    //     latitude: res.latitude + 0.1,
    //     longitude: res.longitude + 0.1,
    //     width: 20,
    //     height: 30,
    //     alpha: 0.8,
    //     callout: {
    //       content: " 终点 ",
    //       color: "#ffffff",
    //       fontSize: 10,
    //       borderRadius: 10,
    //       bgColor: "#6e707c",
    //       padding: 5,
    //       display: "ALWAYS"
    //     }
    //   }
    //   ],
    // });
  },
  // getCenterLocation(){
  //   request({
  //     url: '/GetDriverAddress',
  //     method: 'POST',
  //     data: {
  //       // orderId:wx.getStorageSync('ddbh'),
  //       // customerId: wx.getStorageSync('userCode'),
  //       // outboundorderId: wx.getStorageSync('outboundorderId')

  //       outboundorderId: 'WZ00000001',
  //       orderId: 'XSAA0000590677',
  //       customerId: '7'

  //     }
  //   }).then(res => {
  //     if (res.data.Code === 0) {
  //       console.log(res.data.Source[0])
  //       let markersMap = res.data.Source[0];
  //       this.setData({
  //         markers: [
  //           {
  //             iconPath: "/images/map.png",
  //             id: 0,
  //             latitude: markersMap.stalat,
  //             longitude: markersMap.stalng,
  //             width: 30,
  //             height: 30,
  //             alpha: 0.8,
  //             callout: {
  //               content: " 起始位置 ",
  //               color: "#ffffff",
  //               fontSize: 10,
  //               borderRadius: 10,
  //               bgColor: "#6e707c",
  //               padding: 5,
  //               display: "ALWAYS"
  //             }
  //           },
  //           {
  //             iconPath: "/images/map.png",
  //             id: 1,
  //             latitude: markersMap.endlat,
  //             longitude: markersMap.endlng,
  //             width: 20,
  //             height: 30,
  //             alpha: 0.8,
  //             callout: {
  //               content: " 终点 ",
  //               color: "#ffffff",
  //               fontSize: 10,
  //               borderRadius: 10,
  //               bgColor: "#6e707c",
  //               padding: 5,
  //               display: "ALWAYS"
  //             }
  //           }
  //         ],
  //         })
  //       }

  //     })
  // }


  getLatLng:function(){
    request({
      url: '/GetDriverAddress',
      method: 'POST',
      data: {
        // orderId:wx.getStorageSync('ddbh'),
        // customerId: wx.getStorageSync('userCode'),
        // outboundorderId: wx.getStorageSync('outboundorderId')

        outboundorderId: 'WZ00000001',
        orderId: 'XSAA0000590677',
        customerId: '7'

      }
    }).then(res1 => {
      console.log(res1)
      let markersMap = res1.data.Source[0];
      console.log("1111111111111111")
      console.log(markersMap)
      this.setData({
        markers: [
          {
            iconPath: "/images/map.png",
            id: 0,
            latitude: markersMap.stalat,
            longitude: markersMap.stalng,
            width: 30,
            height: 30,
            alpha: 0.8,
            callout: {
              content: " 起始位置 ",
              color: "#ffffff",
              fontSize: 10,
              borderRadius: 10,
              bgColor: "#6e707c",
              padding: 5,
              display: "ALWAYS"
            }
          },
          {
            iconPath: "/images/map.png",
            id: 1,
            latitude: markersMap.endlat,
            longitude: markersMap.endlng,
            width: 20,
            height: 30,
            alpha: 0.8,
            callout: {
              content: " 终点 ",
              color: "#ffffff",
              fontSize: 10,
              borderRadius: 10,
              bgColor: "#6e707c",
              padding: 5,
              display: "ALWAYS"
            }
          },
          {
            iconPath: "/images/cart.png",
            id: 1,
            latitude: markersMap.lat,
            longitude: markersMap.lng,
            width: 20,
            height: 30,
            alpha: 0.8,
            callout: {
              content: " 货车 ",
              color: "#ffffff",
              fontSize: 10,
              borderRadius: 10,
              bgColor: "#6e707c",
              padding: 5,
              display: "ALWAYS"
            }
          }
        ],
      })
    })
  }
});
<!--pages/WLXiangqing/WLXiangqing.wxml-->
<view class="mapHeight">
  <map 
    id="map" 
    class="map"
    scale='16'
    polyline="{{polyline}}"
    markers="{{markers}}" 
    include-points="{{markers}}"
   ></map>
</view>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值