地名マップ (QML)
Places Mapの例では、MapItemView を使って地図上の場所を検索し、そのリストを表示する方法を示します。
この例では、現在地の地図を表示し、現在地がない場合はオスロ/ノルウェーを使用しています。その後、"food "という用語にマッチする場所の検索が実行され、それぞれの結果が地図上に表示されます。
例の実行
から例を実行するには Qt Creatorから例を実行するには、Welcome モードを開き、Examples から例を選択する。詳細については、Building and Running an Exampleを参照。
ローカル検索
地図上に場所を表示する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
のものであると仮定している。その結果、私たちは常にrole という場所にアクセスできると仮定しています。他の検索結果タイプは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 } } }
© 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.