批量计算自驾通勤距离网页程序

网极科技2个月前代码笔记96

近日给客户做了一个网页小工具,方便客户批量计算通勤距离

运行截图:

html代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>批量通勤距离计算</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
        }
        textarea {
            width: 100%;
            height: 150px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            margin-bottom: 20px;
        }
        button {
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 20px;
        }
        .result table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }
        .result th, .result td {
            border: 1px solid #ccc;
            padding: 10px;
            text-align: left;
        }
    </style>
</head>
<body>
    <div>
        <h1>批量通勤距离计算</h1>
        <textarea id="inputData" placeholder="每行输入一组起点和终点,用逗号分隔。例如:
北京市海淀区上地十街10号,北京市朝阳区三里屯路
上海市浦东新区陆家嘴,上海市徐家汇"></textarea>
        <button onclick="calculateDistance()">计算距离</button>
        <div>
            <h2>计算结果</h2>
            <table id="resultTable">
                <thead>
                    <tr>
                        <th>起点</th>
                        <th>终点</th>
                        <th>距离(公里)</th>
                        <th>预计时间(分钟)</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- 结果将动态插入到这里 -->
                </tbody>
            </table>
        </div>
    </div>

    <script>
        // 计算距离
        async function calculateDistance() {
            const inputData = document.getElementById('inputData').value;

            // 解析输入数据
            const lines = inputData.split('\n').filter(line => line.trim() !== '');
            const data = lines.map(line => {
                const [origin, destination] = line.split(',').map(item => item.trim());
                return { origin, destination };
            });

            // 检查是否有有效数据
            if (data.length === 0) {
                alert('请输入至少一组起点和终点!');
                return;
            }

            // 清空结果表
            const resultTbody = document.querySelector('#resultTable tbody');
            resultTbody.innerHTML = '';

            // 逐条处理数据
            for (let i = 0; i < data.length; i++) {
                const item = data[i];

                try {
                    // 发送单条数据到后端
                    const response = await fetch('calculate_distance.php', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify(item)
                    });

                    // 解析响应
                    const result = await response.json();

                    // 显示结果
                    const row = document.createElement('tr');
                    row.innerHTML = `
                        <td>${result.origin}</td>
                        <td>${result.destination}</td>
                        <td>${result.distance || '-'}</td>
                        <td>${result.duration || '-'}</td>
                    `;
                    resultTbody.appendChild(row);
                } catch (error) {
                    console.error('请求失败:', error);
                    alert('请求失败,请稍后重试。');
                }
            }
        }
    </script>
</body>
</html>

php代码

已隐藏部分内容,支付后自动显示
如有疑问请联系QQ:706448591
支付3元查看


相关文章

如何解决服务器每天固定时间点流量带宽异常?

如何解决服务器每天固定时间点流量带宽异常?

处理服务器流量高峰问题服务器在每天固定的时间点流量消耗很大,这可能是因为这个时间段是用户活跃度的高峰期,导致请求量猛增,让服务器承受不小的压力。但是,如果没有进行有效的流量管理和负载均衡,这种情况可能...

使用xunsearch进行php全文检索

使用xunsearch进行php全文检索

迅搜(xunsearch)是采用 C/C++ 基于 xapian 和 scws 开发的全文搜索引擎解决方案,适用于php全文检索、mysql全文检索和各种站内搜索。支持海量数据高速检索,功能强大、简单...

网页前端使用阿里巴巴普惠字体的方法

网页前端使用阿里巴巴普惠字体的方法

《阿里巴巴普惠体》是一款由中国企业首次发布的可面向全场景使用的免费商用正文字体。可永久免费正版商用的阿里巴巴普惠体,中文字体多采用现代简洁的笔画,造型上偏瘦长,并将字的重心提高。字体笔画上也做了一些...

省市区四级地址编码下载_国家标准行政区划代码映射表【最后更新于2025年1月】

省市区四级地址编码下载_国家标准行政区划代码映射表【最后更新于2025年1月】

产品介绍:省市区四级地址编码_国家标准行政区划代码映射表最后更新于2025年1月产品概述本产品为 省市区四级地址编码与国家标准行政区划代码映射表,旨在为用户提供全面、准确、标准化的行政区划代...

利用php函数获取中文汉字拼音首字母

利用php函数获取中文汉字拼音首字母

要获取中文汉字拼音的首字母,你可以使用第三方库,例如 pinyin。以下是使用 pinyin 库的示例代码: 首先,你需要安装 pinyin 库。你可以通过 Composer 进行安装,在项目根目录...

【精选】PHP将股票日K线数据转换为季K线数据

【精选】PHP将股票日K线数据转换为季K线数据

使用PHP来将股票日K线数据转换为季度K线数据。具体实现方法如下:获取日K线数据,例如从数据库中查询出所有日期的股票数据,保存在一个数组中,每个元素包含如下信息: $day_data = [...