R
R
Roman Pruidze2020-01-23 15:00:35
Python
Roman Pruidze, 2020-01-23 15:00:35

How to insert an OSM map or any other into a PyQt5 application?

Hello, it is necessary to integrate the OSM map or any other into the application on PyQt5. Until the application is assembled into an executable file with osm, everything works, but after building it in pyInstaller and running the exe, it says: "The geoservices provider is not supported." I will be very grateful for your help.

from PyQt5 import QtWidgets, QtCore, QtQuickWidgets, QtPositioning
from PyQt5.QtWidgets import QApplication, QWidget
import uf
import  sys, os

class MarkerModel(QtCore.QAbstractListModel):
    PositionRole, SourceRole = range(QtCore.Qt.UserRole, QtCore.Qt.UserRole + 2)

    def __init__(self, parent=None):
        super(MarkerModel, self).__init__(parent)
        self._markers = []

    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self._markers)

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if 0 <= index.row() < self.rowCount():
            if role == MarkerModel.PositionRole:
                return self._markers[index.row()]["position"]
            elif role == MarkerModel.SourceRole:
                return self._markers[index.row()]["source"]
        return QtCore.QVariant()

    def roleNames(self):
        return {MarkerModel.PositionRole: b"position_marker", MarkerModel.SourceRole: b"source_marker"}

    def appendMarker(self, marker):
        self.beginInsertRows(QtCore.QModelIndex(), self.rowCount(), self.rowCount())
        self._markers.append(marker)
        self.endInsertRows()


class MapWidget(QtQuickWidgets.QQuickWidget):
    def __init__(self, parent=None):
        super(MapWidget, self).__init__(parent,
            resizeMode=QtQuickWidgets.QQuickWidget.SizeRootObjectToView)
        self.model = MarkerModel(self)
        self.rootContext().setContextProperty("markermodel", self.model)
        qml_path = os.path.join(os.path.dirname(__file__), "Untitled-1.qml")
        self.setSource(QtCore.QUrl.fromLocalFile(qml_path))
    
    def place_marks(self, positions):
        urls = ["http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_gray.png", 
            "http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_red.png"]
        for c, u in zip(positions, urls):
            coord = QtPositioning.QGeoCoordinate(*c)
            source = QtCore.QUrl(u)
            self.model.appendMarker({"position": coord , "source": source})

class Window(QtQuickWidgets.QQuickWidget):
    def __init__(self, parent= None):
        QtWidgets.QMainWindow.__init__(self,parent)
        self.ui = uf.Ui_Form()
        self.ui.setupUi(self)
        self.a = MapWidget()
        self.ui.gridLayout.addWidget(self.a)

if __name__ == "__main__": 
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())


uf.py
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'uf.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.gridLayoutWidget = QtWidgets.QWidget(Form)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(20, 30, 361, 241))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question