Minimale Karte (QML)

Der minimale Code zur Anzeige einer Karte mit Qt Quick.

Minimal Map demonstriert die Verwendung des Elements Map zur Darstellung einer Karte. Es zeigt den minimalen Code, der zur Anzeige der Karte benötigt wird, und kann als Grundlage für weitere Experimente verwendet werden.

Ausführen des Beispiels

Zum Ausführen des Beispiels von Qt Creatorauszuführen, öffnen Sie den Modus Welcome und wählen Sie das Beispiel aus Examples. Weitere Informationen finden Sie unter Erstellen und Ausführen eines Beispiels.

C++-Code

In main.cpp verwenden wir nur die Klassen QGuiApplication und QQmlApplicationEngine.

#include <QGuiApplication>
#include <QQmlApplicationEngine>

In der Hauptfunktion instanziieren wir zunächst ein QGuiApplication Objekt. Dann erstellen wir ein QQmlApplicationEngine und weisen es an, main.qml aus dem Qt Resource System zu laden.

Schließlich wird mit QGuiApplication::exec() die Hauptereignisschleife gestartet.

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

QML-Code

In main.qml importieren wir das QtLocation QML-Modul und das von ihm abhängige QtPositioning QML-Modul. Als nächstes erstellen wir das Fenster der obersten Ebene, legen eine sinnvolle Standardgröße fest und machen es sichtbar. Das Fenster wird durch ein Element Map gefüllt, das die Karte zeigt.

import QtQuick
import QtLocation
import QtPositioning

Window {
    width: Qt.platform.os == "android" ? Screen.width : 512
    height: Qt.platform.os == "android" ? Screen.height : 512
    visible: true
    title: map.center + " zoom " + map.zoomLevel.toFixed(3)
           + " min " + map.minimumZoomLevel + " max " + map.maximumZoomLevel

    Plugin {
        id: mapPlugin
        name: "osm"
    }

    Map {
        id: map
        anchors.fill: parent
        plugin: mapPlugin
        center: QtPositioning.coordinate(59.91, 10.75) // Oslo
        zoomLevel: 14
        property geoCoordinate startCentroid

        PinchHandler {
            id: pinch
            target: null
            onActiveChanged: if (active) {
                map.startCentroid = map.toCoordinate(pinch.centroid.position, false)
            }
            onScaleChanged: (delta) => {
                map.zoomLevel += Math.log2(delta)
                map.alignCoordinateToPoint(map.startCentroid, pinch.centroid.position)
            }
            onRotationChanged: (delta) => {
                map.bearing -= delta
                map.alignCoordinateToPoint(map.startCentroid, pinch.centroid.position)
            }
            grabPermissions: PointerHandler.TakeOverForbidden
        }
        WheelHandler {
            id: wheel
            // workaround for QTBUG-87646 / QTBUG-112394 / QTBUG-112432:
            // Magic Mouse pretends to be a trackpad but doesn't work with PinchHandler
            // and we don't yet distinguish mice and trackpads on Wayland either
            acceptedDevices: Qt.platform.pluginName === "cocoa" || Qt.platform.pluginName === "wayland"
                             ? PointerDevice.Mouse | PointerDevice.TouchPad
                             : PointerDevice.Mouse
            rotationScale: 1/120
            property: "zoomLevel"
        }
        DragHandler {
            id: drag
            target: null
            onTranslationChanged: (delta) => map.pan(-delta.x, -delta.y)
        }
        Shortcut {
            enabled: map.zoomLevel < map.maximumZoomLevel
            sequence: StandardKey.ZoomIn
            onActivated: map.zoomLevel = Math.round(map.zoomLevel + 1)
        }
        Shortcut {
            enabled: map.zoomLevel > map.minimumZoomLevel
            sequence: StandardKey.ZoomOut
            onActivated: map.zoomLevel = Math.round(map.zoomLevel - 1)
        }
    }
}

Das Element Plugin ist notwendig, um den Kartenanbieter zu definieren, den wir verwenden werden. Das Beispiel kann mit jedem der verfügbaren Geodienste-Plugins funktionieren. Einige Plugins können jedoch zusätzliche Plugin-Parameter erfordern, um korrekt zu funktionieren, und wir können PluginParameter verwenden, um sie anzugeben. In diesem Beispiel verwenden wir das Plugin osm, das ein Qt Location Open Street Map Plugin ist und keine Parameter benötigt.

Im Element Map verweisen wir auf das von uns verwendete plugin und legen center und zoomLevel der Karte fest.

Anforderungen

Das Beispiel erfordert eine funktionierende Internetverbindung, um OpenStreetMap Kartenkacheln herunterzuladen. Ein optionaler System-Proxy sollte automatisch abgeholt werden.

Beispielprojekt @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.