장소 맵(QML)

장소 맵 예제에서는 MapItemView 을 사용하여 지도에서 장소 목록을 검색하고 표시하는 방법을 보여줍니다.

이 예에서는 현재 위치의 지도를 표시하거나 사용할 수 있는 위치가 없는 경우 오슬로/노르웨이를 사용합니다. 그런 다음 '음식'이라는 용어와 일치하는 장소를 검색하고 각 결과를 지도에 표시합니다.

예제 실행하기

에서 예제를 실행하려면 Qt Creator에서 Welcome 모드를 열고 Examples 에서 예제를 선택합니다. 자세한 내용은 예제 구축 및 실행하기를 참조하세요.

지도에 장소를 표시하는 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
        }
    }
}

예제 프로젝트 @ 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.