share.js 6.02 KB
const restapi = require('../restapi.js')
const util = require('../../../../utils/util.js')
const mapapi = require('../../../../utils/mapapi.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    marginTop: 0,
    mapHeight: wx.getSystemInfoSync().windowHeight - 50,
    flag: true,
    longitude: 0,
    latitude: 0,
    markers: [],
    polyline: [],
    beginAddress: '',
    endAddress: '',
    beginTime: '',
    endTime: '',
    drivingTotalTime: '',
    averageSpeed: '',
    hasData: true,
    startDate: '',
    endDate: '',
    mileage: '',
    points: [],
    include_points: []
  },
  getNavHeight(e) {
    this.setData({
      marginTop: e.detail
    })
  },
  // 动态改变地图高度
  changeHeight() {
    let phoneHeight = wx.getSystemInfoSync().windowHeight
    let query = wx.createSelectorQuery().in(this)
    query.select('#console').boundingClientRect(res => {
      this.setData({
        mapHeight: (phoneHeight - res.height)
      })
    }).exec()
  },
  getData() {
    let userId = app.globalData.selectedDevice.userId;
    let journeyId = this.data.journeyId;
    let startDate = this.data.startDate;
    let endDate = this.data.endDate;
    console.log("getJourneyById");
    console.log("userId", userId);
    console.log("journeyId", journeyId);
    console.log("startDate", startDate);
    console.log("endDate", endDate);
    restapi.getJourneyById(journeyId, res => {
      console.log(res)
      if (res.returnCode == '0' && res.data) {
        this.setData({
          hasData: false,
          flag: false
        })
        let data = res.data
        let duration = (data.endTime - data.beginTime) / 1000
        let points = []

        data.gpsList.forEach(e => {
          points.push({
            longitude: e.gLng,
            latitude: e.gLat
          })
        })
        let markers = [{
          id: 0,
          latitude: points[0].latitude,
          longitude: points[0].longitude,
          iconPath: '../../resources/images/start.png',
          width: '80rpx',
          height: '80rpx',
          anchor: {
            x: 0.5,
            y: 1
          }
        },
        {
          id: 1,
          latitude: points[points.length - 1].latitude,
          longitude: points[points.length - 1].longitude,
          iconPath: '../../resources/images/end.png',
          width: '80rpx',
          height: '80rpx',
          anchor: {
            x: 0.5,
            y: 1
          }
        },
          // {
          //   id: 2,
          //   latitude: points[0].latitude,
          //   longitude: points[0].longitude,
          //   iconPath: '../../resources/images/car.png',
          //   width: '38rpx',
          //   height: '86rpx',
          //   anchor: {
          //     x: 0.5,
          //     y: 1
          //   }
          // }
        ]
        let polyline = [{
          points: points,
          width: 5,
          color: '#1CB954',
          borderColor: '#029135',
          borderWidth: '1'
        }]

        let st = data.beginTime / 1000
        let et = data.endTime / 1000
        let speedData = this.driveParamCompute(st, et, data.mileage)
        console.log(speedData)
        this.setData({
          longitude: points[0].longitude,
          latitude: points[0].latitude,
          beginAddress: data.beginAddress,
          endAddress: data.endAddress,
          beginTime: new Date(data.beginTime).format('yyyy-MM-dd hh:mm'),
          endTime: new Date(data.endTime).format('yyyy-MM-dd hh:mm'),
          markers: markers,
          polyline: polyline,
          mileage: speedData.dis,
          averageSpeed: speedData.speed,
          include_points: points
        })
        this.changeHeight()
        // this.mapUpdated()
      } else {
        this.setData({
          flag: true
        })
      }
    })
  },
  mapUpdated() {
    const map = wx.createMapContext('map')
    let points = this.data.points
    console.log(points)
    if (this.data.points) { }
    points.forEach(e => {
      this.translateMarker(map, {
        longitude: e.longitude,
        latitude: e.latitude
      })
    })
  },
  translateMarker(map, destination) {
    map.translateMarker({
      markerId: 2,
      destination: {
        longitude: destination.longitude,
        latitude: destination.latitude
      },
      autoRotate: false,
      rotate: 12,
      duration: 100
    })
  },
  driveParamCompute(st, et, mileage) {
    let dis = (mileage / 1000).toFixed(2)
    let time = ((et - st) / 3600).toFixed(2)
    let sp = (dis / time).toFixed(2)
    let data = {
      dis: dis,
      speed: (dis / time).toFixed(2)
    }
    return data
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("options", options);
    let drivingTotalTime = decodeURIComponent(options.drivingTotalTime);
    let startDate = decodeURIComponent(options.startDate);
    let endDate = decodeURIComponent(options.endDate);
    console.log('decodeURIComponent:', drivingTotalTime, startDate, endDate)
    this.setData({
      journeyId: options.journeyId,
      drivingTotalTime: drivingTotalTime,
      startDate: startDate,
      endDate: endDate
    })
    this.getData();
    this.changeHeight()
    setTimeout(() => {
      if (this.data.hasData) {
        this.setData({
          hasData: false,
          flag: true
        })
      }
    }, 8000)
  },

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

  },

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

  },

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

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log("unload...")
    this.setData({
      polyline: [],
      markers: [],
      mileage: '',
      averageSpeed: '',
      include_points: []
    })
  },

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

  },

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

  }
})