このページでは

ミニマルマップ (QML)

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

オスロ中心部の地図が表示されたアプリケーションウィンドウ

「Minimal Map」は、Map アイテムを使用して地図をレンダリングする方法を示しています。地図を表示するために必要な最小限のコードを示しており、さらなる実験の基礎として利用できます。

例の動作確認

以下の手順でサンプルを実行できます:

C++ コード

main.cpp では、QGuiApplication クラスとQQmlApplicationEngine クラスのみを使用します。

#include <QGuiApplication>
#include <QQmlApplicationEngine>

main関数では、まずQGuiApplication オブジェクトをインスタンス化します。次に、QQmlApplicationEngine を作成し、Qtリソースシステムから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 のOpenStreetMapプラグインである osm プラグインを使用しており、パラメータは一切必要ありません。

「Map 」項目では、使用するplugin を参照し、マップのcenter とzoomLevel を設定します。

要件

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

code.qt.io上のサンプルプロジェクト

© 2026 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.