python如何绘制地铁线路图

Python相传是无所不能的,但是最近想做一个地铁图的查看功能,但是查阅了一下百度地图开发者平台,发现并没有支持python的代码

  1. 使用准备
    1.python 3.x
    2.PyQt5
    3.JavaScript
    4.BaiDu ak

  2. 第一步:创建百度地图密钥
    登录并打开百度地图控制台
    python如何绘制地铁线路图_第1张图片

python如何绘制地铁线路图_第2张图片

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>地铁图展示</title>
<script type="text/javascript" src="https://api.map.baidu.com/api?type=subway&v=1.0&ak=你的密钥"></script>
<style type="text/css">
#container{height:100%}
</style>
</head>
<body>
<div id="container"></div>
<script type="text/javascript">

var subwayCityName = '广州';
var list = BMapSub.SubwayCitiesList;
var subwaycity = null;
for (var i = 0; i < list.length; i++) {
if (list[i].name === subwayCityName) {
subwaycity = list[i];
break;
}
}
// 获取北京地铁数据-初始化地铁图
var subway = new BMapSub.Subway('container', subwaycity.citycode);
subway.setZoom(0.5);
</script>
</body>
</html>
将上述代码的密钥换成自己的,并另存为subway.html
  1. 下载所需要的python库
import os
import threading

def first():
	os.system("pip3 install PyQtWebEngine-i https://pypi.douban.com/simple")
def second():
	os.system("pip install PyQt5 -i https://pypi.douban.com/simple")

downloadThreads = []
downloadThread1 = threading.Thread(target=first)
downloadThread2 = threading.Thread(target=second)
downloadThreads.append(downloadThread1)
downloadThreads.append(downloadThread2)

downloadThread1.start()
downloadThread2.start()

当看见一下代码时,证明安装成功

  1. 调用PyQtWebEngine
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
import sys

class MainWindow(QMainWindow):

	def __init__(self ):
		super(QMainWindow, self).__init__()
		self.setWindowTitle('地铁图查看')
		self.setGeometry(5, 30, 755, 530)
		self.browser = QWebEngineView()   
        # 加载本地页面
		url = r'file:///C:/Users/Administrator/Desktop/subway.html'
		self.browser.load( QUrl( url ))	
		self.setCentralWidget(self.browser)

if __name__ == '__main__':
	app = QApplication(sys.argv)       
	win = MainWindow()
	win.show()
	sys.exit(app.exec_()

运行就可以发现,广州地铁线路图出现在我们的Gui界面上
python如何绘制地铁线路图_第3张图片

你可能感兴趣的:(python如何绘制地铁线路图)