QQ截图20241025150951.png (40.88 KB, 下载次数: 0)
下载附件
2024-10-25 15:15 上传
百度地图API 基于起点及终点GPS数据 分别计算起点到各点的直接距离 导航距离及耗时
(GCJ-02坐标) 奥维直接复制即可
[Python] 纯文本查看 复制代码import sys
import json
import requests
from math import radians, cos, sin, sqrt, atan2
from PyQt5 import QtWidgets
class MapApp(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.init_ui()
self.load_config()
def init_ui(self):
self.setWindowTitle("百度地图多点路径计算")
self.api_key_label = QtWidgets.QLabel("API密钥:")
self.api_key_input = QtWidgets.QLineEdit(self)
self.origin_label = QtWidgets.QLabel("起点 (地点|经度,纬度):")
self.origin_input = QtWidgets.QLineEdit(self)
self.destination_label = QtWidgets.QLabel("终点 (每行一个地点|经度,纬度):")
self.destination_input = QtWidgets.QTextEdit(self)
self.calculate_button = QtWidgets.QPushButton("计算路径", self)
self.calculate_button.clicked.connect(self.calculate_routes)
self.save_button = QtWidgets.QPushButton("保存配置", self)
self.save_button.clicked.connect(self.save_config)
# 调试信息文本框
self.debug_text = QtWidgets.QTextEdit(self)
self.debug_text.setReadOnly(True)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.api_key_label)
layout.addWidget(self.api_key_input)
layout.addWidget(self.origin_label)
layout.addWidget(self.origin_input)
layout.addWidget(self.destination_label)
layout.addWidget(self.destination_input)
layout.addWidget(self.calculate_button)
layout.addWidget(self.save_button)
layout.addWidget(self.debug_text) # 将调试文本框添加到布局中
self.setLayout(layout)
def load_config(self):
try:
with open('config.json', 'r') as file:
config = json.load(file)
self.api_key_input.setText(config.get("api_key", ""))
self.origin_input.setText(config.get("origin", ""))
self.destination_input.setPlainText(config.get("destination", ""))
except FileNotFoundError:
self.debug_text.append("未找到配置文件,加载默认值。")
def save_config(self):
config = {
"api_key": self.api_key_input.text(),
"origin": self.origin_input.text(),
"destination": self.destination_input.toPlainText(),
}
with open('config.json', 'w') as file:
json.dump(config, file)
self.debug_text.append("配置已保存。")
def parse_location(self, location_str):
"""解析'地点|经度,纬度'格式并返回名称、经度和纬度"""
try:
name, coords = location_str.split('|')
lng, lat = coords.split(',')
return name, float(lat), float(lng)
except ValueError:
self.debug_text.append(f"解析错误: 无效的格式 '{location_str}'")
return None, None, None
def calculate_straight_distance(self, lat1, lon1, lat2, lon2):
"""计算两个经纬度之间的直线距离(单位:公里)"""
R = 6371.0 # 地球半径,单位为公里
dlat = radians(lat2 - lat1)
dlon = radians(lon2 - lon1)
a = sin(dlat / 2)**2 + cos(radians(lat1)) * cos(radians(lat2)) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = R * c
return round(distance, 2)
def format_duration(self, duration_seconds):
"""格式化导航时间,当时间小于1小时时显示分钟,否则显示小时"""
duration_hours = duration_seconds / 3600
if duration_hours
回复可见成品下载地址
游客,如果您要查看本帖隐藏内容请回复