计算起点地址和终点地址的最短驾车距离和驾车时间

186 篇文章 4 订阅
62 篇文章 2 订阅

微信小程序开发交流qq群   173683895

   承接微信小程序开发。扫码加微信。

需求:

在一个excel的xlsx表格中有很多起点的地址和终点的地址,要批量计算两个地址之间的距离和驾车时间,按照百度地图的最短距离计算。最后把得出的行驶距离和驾车时间填充至xlsx表格中的相应地址

实现思路:

1.把excel表格中的数据导入数据库

2.通过接口请求数据库,得到所有的起始地址信息并且保存到前端

3.把所有的中午地址转换成经纬度并且存入数据库(因为中午地址是无法直接计算出行驶距离和驾车时间的)

4.请求数据库中的起始点经纬度,计算出行驶距离和驾车时间再保存到数据库

5.数据库导出到excel的xlsx表格,完成

生产场景:

微信小程序开发者工具 + phpstudy + mysql + 百度地图开放平台 - JavaScript API (两个接口,逆/地址解析 和 路线规划

先建数据库, 结构图如下:产数分别是起点地址,终点地址,距离,时间

小程序前端源码:

var util = require('../../utils/util.js');
// pages/index/index.js
var aa_data, i = 1271,
  y = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  // 保存到数据库
  ddddddd(distance, duration) {
    var that = this;
    var data = {
      duration: duration,
      distance: distance,
      sss: this.data.data[i].sss,
      eee: this.data.data[i].eee,
    }
    wx.request({
      url: 'http://localhost/excel/save.php',
      data: data,
      success: function(res) {
        
      }
    })

  },
  // 计算起点坐标和终点坐标的最短驾车距离和驾车时间
  cccccccc(e) {
    var that = this;
    var aaa = e.sss.split(',')
    console.log(aaa)
    var a_ssss = aaa[1].substring(0, 10) + ',' + aaa[0].substring(0, 10)
    if (e.eee=='-'){

      setTimeout(function () {
        i = i + 1;
        console.log(i)
        // that.cccccccc(aa_data[i])
      }, 300)
      return
    }
    var bbb = e.eee.split(',')
    console.log(a_ssss)
    var a_eeee = bbb[1].substring(0, 10) + ',' + bbb[0].substring(0, 10)
    wx.request({
      url: 'http://api.map.baidu.com/direction/v2/driving?origin=' + a_ssss + '&destination=' + a_eeee + '&ak=MGIf4Hhwkx9GWrjDh2cVGKk1hQr5tcb8',
      data: {},
      success: function(res) {
        var distance = res.data.result.routes[0].distance
        var duration = res.data.result.routes[0].duration
        console.log('最佳路线:', distance + '米', parseInt(duration / 60) + '分钟')
        that.ddddddd(distance, parseInt(duration / 60))

        setTimeout(function () {
          i = i + 1;
          console.log(i)
          that.cccccccc(aa_data[i])
        }, 300)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.request({
      url: 'http://localhost/excel/show.php',
      data: {
        location
      },
      success: function(res) {
        console.log(res.data)
        aa_data = res.data
        that.cccccccc(aa_data[i])
        // that.bbbbbb(aa_data[i].sss)

        that.setData({
          data: res.data
        })
      }
    })
  },
  // 地址转换成经纬度
  bbbbbb(add) {
    var that = this;
    var url = 'http://api.map.baidu.com/geocoder/v2/';
    var data = {
      address: add.length > 35 ? add.substring(0, 35) : add,
      output: 'json',
      ak: 'LGPktyrfCarUmfPwDDrL1nG6mlhlkLwA'
    }
    util.request(url, 'post', data, '', function(res) {

      var location = res.data.result.location;
      console.log(location);

      wx.request({
        url: 'http://localhost/excel/sss.php',
        data: {
          sss: location.lng + ',' + location.lat,
          add
        },
        success: function(res) {
          console.log(res.data);
          console.log(i)
          i = i + 1;
          if (i < aa_data.length) {
            setTimeout(function() {
              if (aa_data[i].sss == '-') {
                i = i + 1;
                that.bbbbbb(aa_data[i].sss)
              } else {
                that.bbbbbb(aa_data[i].sss)
              }
            }, 300)
          } else {}

        }
      })
    })
  },
  // excel导入数据库
  aaaaaaaa() {
    wx.request({
      url: 'http://localhost/excel/excel.php',
      data: {
        location
      },
      success: function(res) {
        console.log(res.data)
      }
    })
  },
})
<!--pages/index/index.wxml-->
<button bindtap='aaaaaaaa'>加载到数据库</button>
<button bindtap='cccccccc'>经纬度计算出时间,路程</button>

后端源码下载 https://download.csdn.net

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a_靖

对你有帮助吗?打赏鼓励一下?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值