pathDetail.js 3.56 KB
// packageIndex/pages/gps/pathDetail/pathDetail.js
import {
  carlifeApi
} from '../../../../api/api'
import mapApi from '../../../../utils/mapapi'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    controlHeight: 0,
    lineHeight: 0,
    detail: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.$router.getData(id => {
      this.getPathDetail(id)
    })
  },
  getPathDetail(id) {
    wx.$util.showLoading('加载中...')
    carlifeApi.getPathDetail(id).then(({
      data,
      message
    }) => {
      wx.hideLoading()
      if (!data) {
        wx.showModal({
          title: '提示',
          content: message || '系统异常',
          showCancel: false,
          confirmText: '重试',
          confirmColor: '#FC5B32'
        }).then(res => {
          if (res.confirm) this.getPathDetail(id)
        })
        return
      }
      data.time = wx.$util.getScopeTime(data.beginTime, data.endTime)
      data.beginTime = new Date(data.beginTime).format('yyyy-MM-dd hh:mm:ss')
      data.endTime = new Date(data.endTime).format('yyyy-MM-dd hh:mm:ss')
      data.meanSpeed = (data.mileage / data.totalTime * 3600 / 1000).toFixed(2)
      data.mileage = (data.mileage / 1000).toFixed(2)
      this.setData({
        flag: true,
        detail: data
      }, () => {
        this.setLineHeight()
      })
    })
  },
  getControlHeight() {
    wx.$util.getViewInfo('.control-box', ({
      height
    }) => {
      this.setData({
        controlHeight: height
      })
      this.drawMapMarkers()
    })
  },
  drawPolyline() {
    let points = []
    const {
      gpsList
    } = this.data.detail
    gpsList.map(e => {
      points.push({
        longitude: Number(e.glng),
        latitude: Number(e.glat)
      })
    })
    this.setData({
      polyline: [{
        points,
        color: '#649EFA',
        width: 8,
        borderColor: '#327FFA',
        borderWidth: 2,
      }]
    })
    wx.hideLoading()
  },
  drawMapMarkers() {
    const {
      detail,
      IMGURL,
      img_version
    } = this.data
    const map = wx.createMapContext('map')
    mapApi.translate([`${detail.beginGpsLat},${detail.beginGpsLon}`, `${detail.endGpsLat},${detail.endGpsLon}`], 1).then(({
      locations
    }) => {
      let markers = []
      let points = []
      locations.map((e, index) => {
        let latitude = e.lat
        let longitude = e.lng
        markers.push({
          iconPath: `${IMGURL}packageIndex/gps/${index == 0?'begin':'end'}.png?v=${img_version}`,
          latitude,
          longitude,
          width: '27px',
          height: '32px'
        })
        points.push({
          latitude,
          longitude
        })
      })
      map.addMarkers({
        markers,
        clear: true
      })
      map.includePoints({
        points
      })
    })
    this.drawPolyline()
  },
  setLineHeight() {
    wx.$util.getViewInfo('.from', ({
      height
    }) => {
      this.setData({
        lineHeight: height
      })
      this.getControlHeight()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})