QtLocation.qtlocation-places-list-example

src="https://assets.ubuntu.com/v1/b7976ed5-places-list.png" alt="" />

To write a QML application that will show places in a list, we start by making the following import declarations.

import QtQuick 2.0
import QtPositioning 5.2
import QtLocation 5.3

Instantiate a Plugin instance. The Plugin is effectively the backend from where places are sourced from. Because the nokia plugin has been specified, some mandatory parameters need to be filled in, see the Nokia Plugin documentation for details:

Plugin {
id: myPlugin
name: "nokia"
//specify plugin parameters as necessary
//PluginParameter {...}
//PluginParameter {...}
//...
}

Next we instantiate a PlaceSearchModel which we can use to specify search parameters and perform a places search operation. For illustrative purposes, update() is invoked once construction of the model is complete. Typically update() would be invoked in response to a user action such as a button click.

PlaceSearchModel {
id: searchModel
plugin: myPlugin
searchTerm: "pizza"
searchArea: QtPositioning.circle(startCoordinate);
Component.onCompleted: update()
}

Finally we instantiate a ListView to show the search results found by the model. An inline delegate has been used and we have assumed that every search result is of type PlaceSearchesult. Consequently it is assumed that we always have access to the place role, other search result types may not have a place role.

ListView {
anchors.fill: parent
model: searchModel
delegate: Component {
Column {
Text { text: title }
Text { text: place.location.address.text }
}
}
spacing: 10
}

Files:

  • places_list/places_list.qml
  • places_list/main.cpp
  • places_list/places_list.pro