このページについて

場所マップ (QML)

「場所マップ」のサンプルでは、MapItemView を使用して、場所を検索し、マップ上に一覧表示する方法を示しています。

ピザ店の場所を示すラベル付きのマーカーがいくつかある地図

このサンプルでは、現在地を示す地図が表示されます。現在地が取得できない場合は、ノルウェーのオスロが使用されます。その後、「food」という検索語句に一致する場所の検索が行われ、各検索結果が地図上に表示されます。

例の動作

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

地図上に場所を表示する QML アプリケーションを作成するには、まず以下のインポート宣言を行います。

import QtQuick
import QtPositioning
import QtLocation

Plugin インスタンスを生成します。Plugin は、実質的に場所のデータが取得されるバックエンドです。選択したプラグインによっては、必須のパラメータが必要になる場合があります。この例ではOSM プラグインが選択されていますが、これには必須のパラメータはありません。

Plugin {
    id: myPlugin
    name: "osm"
    //specify plugin parameters if necessary
    //PluginParameter {...}
    //PluginParameter {...}
    //...
}

次に、検索パラメータを指定して場所の検索操作を実行するために使用できるPlaceSearchModel をインスタンス化します。説明の便宜上、モデルの構築が完了したらupdate()を一度呼び出します。通常、update()は、ボタンのクリックなどのユーザー操作に応じて呼び出されます。

PlaceSearchModel {
    id: searchModel

    plugin: myPlugin

    searchTerm: "food"
    searchArea:  QtPositioning.circle(positionSource.lastSearchPosition, 1000 /* 1 km radius */)
    Component.onCompleted: update()
}

マップは `MapView ` 型を使用して表示され、その内部で `MapItemView ` を宣言し、検索モデルとデリゲートを指定しています。ここではインラインデリゲートが使用されており、すべての検索結果が `type `PlaceSerachesult 型であると仮定しています。したがって、常に `place` role へのアクセスが可能であると想定されていますが、他の検索結果型には`place` role が存在しない場合があります。

MapView {
    id: view
    anchors.fill: parent
    map.plugin: myPlugin;
    map.center: positionSource.lastSearchPosition
    map.zoomLevel: 13

    MapItemView {
        model: searchModel
        parent: view.map
        delegate: MapQuickItem {
            coordinate: place.location.coordinate

            anchorPoint.x: image.width * 0.5
            anchorPoint.y: image.height

            sourceItem: Column {
                Image { id: image; source: "marker.png" }
                Text { text: title; font.bold: true }
            }
        }
    }
}

最後に、PositionSource を使用して、地図を現在の位置にリセットし、新しいエリア内の「食事」スポットを検索します。位置情報は2分ごとに更新され、新しい位置が前回の食事スポット検索エリアから500メートル以上離れている場合、スポット検索が再実行されます。

PositionSource {
    id: positionSource
    property variant lastSearchPosition: QtPositioning.coordinate(59.93, 10.76) //Initialized/Fallback to Oslo
    active: true
    updateInterval: 120000 // 2 mins
    onPositionChanged:  {
        var distance = lastSearchPosition.distanceTo(position.coordinate)
        if (distance > 500) {
            // 500m from last performed food search
            lastSearchPosition = positionSource.position.coordinate
        }
    }
}

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