最小限の地図 (QML)

Qt Quick を使って地図を表示するための最小限のコードです。

Minimal Mapは Map を使って地図を表示する方法を示しています。地図を表示するのに必要な最小限のコードを示しており、さらなる実験の基礎として使うことができます。

例の実行

から例を実行するには Qt Creatorからサンプルを実行するには、Welcome モードを開き、Examples からサンプルを選択します。詳細については、Building and Running an Exampleを参照してください。

C++コード

main.cpp では、QGuiApplicationQQmlApplicationEngine クラスのみを使用しています。

#include <QGuiApplication>
#include <QQmlApplicationEngine>

メイン関数では、まずQGuiApplication オブジェクトをインスタンス化します。次にQQmlApplicationEngine を作成し、Qt Resource System からmain.qml をロードするように指示します。

最後に、QGuiApplication::exec ()でメインイベントループを開始します。

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

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

    return app.exec();
}

QMLコード

main.qml では、QtLocation QML モジュールと、それに依存するQtPositioning QML モジュールをインポートしています。次に、トップレベルのウィンドウを作成し、適切なデフォルトサイズを設定し、表示できるようにします。ウィンドウは地図を表示するMap アイテムで満たされます。

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)
        }
    }
}

Plugin アイテムは使用するマッププロバイダを定義するために必要です。この例は、利用可能なジオ・サービス・プラグインのどれでも動作します。しかし、プラグインによっては、正しく機能するために追加のプラグインパラメータが必要な場合があり、PluginParameter を使って指定することができます。この例では、Qt Location Open Street Map Pluginであり、パラメータを必要としないosm プラグインを使用します。

Map の項目で、使用するplugin を参照し、地図のcenterzoomLevel を設定します。

必要条件

この例では、OpenStreetMap マップタイルをダウンロードするためのインターネット接続が必要です。オプションのシステムプロキシは自動的にピックアップされます。

サンプルプロジェクト @ 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.