我使用 PyQt5 创建了一个应用程序,该应用程序(以及其他)显示地图。
对于地图小部件,我使用了 pyqtlet,但我开始意识到这个包真的很有限(我想显示不同的图层,使用可拖动的标记等),所以我想转换为ipyleaflet .
除了我无法让我的地图显示在应用程序中!
原来的代码是这样的:
import sys
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget, QLabel
from pyqtlet import L, MapWidget
class MapWindow(QWidget):
def __init__(self, base_coords):
self.base_coords = base_coords
# Setting up the widgets and layout
super().__init__()
self.layout = QVBoxLayout()
self.title = QLabel("<b>This is my title</b>")
self.layout.addWidget(self.title)
self.mapWidget = MapWidget()
self.layout.addWidget(self.mapWidget)
self.setLayout(self.layout)
# Working with the maps with pyqtlet
self.map = L.map(self.mapWidget)
self.map.setView(self.base_coords, zoom=10)
L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}'
).addTo(self.map) # ArcGIS_topo layer
self.marker = L.marker(self.base_coords)
self.marker.bindPopup('This is my marker')
self.map.addLayer(self.marker)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
base_coords = [45.783119, 3.123364]
widget = MapWindow(base_coords)
sys.exit(app.exec_())
然后我尝试使用它来更改为 ipyleaflet:
import sys
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget, QLabel
from PyQt5 import QtWebEngineWidgets
from ipyleaflet import Map, Marker, LayersControl, basemaps
from ipywidgets import HTML
class MapWindow(QWidget):
def __init__(self, base_coords):
self.base_coords = base_coords
# Setting up the widgets and layout
super().__init__()
self.layout = QVBoxLayout()
self.title = QLabel("<b>This is my title</b>")
self.layout.addWidget(self.title)
# Working with the maps with ipyleaflet
self.map = Map(center=self.base_coords, basemaps=basemaps.Esri.WorldTopoMap, zoom=10)
self.layout.addWidget(self.map)
self.marker = Marker(location=self.base_coords)
self.marker.popup = HTML(value='This is my marker')
self.map.add_layer(self.marker)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
base_coords = [45.783119, 3.123364]
widget = MapWindow(base_coords)
sys.exit(app.exec_())
但是在布局中添加地图不起作用,我收到此错误消息:
Traceback (most recent call last):
File "G:\venv\lib\site-packages\IPython\core\interactiveshell.py", line 3418, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-bd466e04ab02>", line 1, in <module>
runfile('G:/Application/short_app - Copie.py', wdir='G:/Application')
File "C:\Program Files\JetBrains\PyCharm 2020.2.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2020.2.3\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "G:/Application/short_app - Copie.py", line 29, in <module>
widget = MapWindow(base_coords)
File "G:/Application/short_app - Copie.py", line 19, in __init__
self.layout.addWidget(self.map)
TypeError: addWidget(self, QWidget, stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'Map'
有人知道我可以将 ipyleaflet 地图添加到我的应用程序吗?