In the Ubuntu SDK, it provides a Placesearchmodel interface. Through this interface, we can search for the places we need in the specified area, such as KFC, Pizzar, or bank. In today's tutorial, we use this API to show how to search for our points of interest.
Basic usage of Placesearchmodel:
Placesearchmodel {
Id:searchmodel
plugin:myplugin
searchterm: "KFC"
Searcharea: Qtpositioning.circle (curlocation)
Component.onCompleted:update ()
}
The searchterm here is the keyword we need to search for. Searcharea is what we are looking at as the center. Whenever there is a new keyword to search for, we can do it in the following ways:
Searchmodel.searchterm = text
searchmodel.update ();
The plugin here are the plugin that already exist in our system. We can actually get all the plugin of the system in the following way:
Plugin {
id:plugin
//Set The default one
component.oncompleted: {
name = Availableserviceproviders[0]
}
}
See the article "using the map API to display maps and dynamically display tags on ubuntu phones".
When a location is successfully searched, it returns the following data:
We can use this data to show us where we get the information.
Based on these understandings, we have made our routines: main.qml
Import QtQuick 2.4 Import ubuntu.components 1.3 Import Ubuntu.Components.Popups 1.3 Import qtlocation 5.3 Import Qtpositio Ning 5.2 MainView {//ObjectName for functional testing purposes (AUTOPILOT-QT5) ObjectName: "MainView"// note!
ApplicationName needs to match the "name" field of the click Manifest ApplicationName: "Placesearchmodel.liu-xiao-guo" Width:units.gu (HEIGHT:UNITS.GU) property bool Centercurrent:true Plugin {Id:myplug In name: ' OSM '} positionsource {Id:positionsource Property variant lastsearchposition: Curlocation active:true updateinterval:5000 onpositionchanged: {var currentpositi
On = PositionSource.position.coordinate if (centercurrent) {map.center = CurrentPosition } var distance = Currentposition.distanceto (lastsearchposition)//if (dist
ance > 500) { 500m from last performed pizza search lastsearchposition = currentposition searchmod
El.searcharea = Qtpositioning.circle (currentposition) searchmodel.update ()//} }} Property Variant CurLocation:QtPositioning.coordinate (59.93, 10.76, 0) Placesearchmodel {ID:
Searchmodel plugin:myplugin searchterm: "KFC" SearchArea:QtPositioning.circle (curlocation)
Component.onCompleted:update ()} Connections {Target:searchmodel onstatuschanged: {
if (Searchmodel.status = = Placesearchmodel.error) Console.log (searchmodel.errorstring ());
}} Component {id:pop Popover {id:popover Width:units.gu (20)
Property var model Column {anchors {left:parent.left Right:parent.rigHT} Label {Anchors.horizontalCenter:parent.horizontalCenter Text: {switch (model.type) {case placesearchmodel.unknowns Earchresult:return "Type:unknownsearchresult" Case Placesearchmodel. Placeresult:return "Type:placeresult" Case placesearchmodel.proposed
Searchresult:return "Type:proposedsearchresult"} } fontSize: "Medium"} Label {Anchors.horizont
AlCenter:parent.horizontalCenter text: "title:" + model.title fontSize: "Medium"
} Label {Anchors.horizontalCenter:parent.horizontalCenter Text: "DistaNCE: "+ (model.distance). toFixed (2) +" M "FontSize:" Medium "} Label {Anchors.horizontalCenter:parent.horizontalCenter text: "Sponsored:" + MODEL.S ponsored fontSize: "Medium"}}}} page {Id:pa GE Header:standardheader pageheader {id:standardheader Visible:page.header = =
= Standardheader Title: "Placesearchmodel" trailingactionbar.actions: [Action { Iconname: "Edit" Text: "edit" OnTriggered:page.header = edithe Ader}]} pageheader {Id:editheader visible:page. Header = = = Editheader Leadingactionbar.actions: [Action {iconname: "Back The TeXT: "Back" ontriggered: {page.header = Standardheader}
}] Contents:textfield {id:input Anchors { Left:parent.left Right:parent.right verticalCenter:parent.vertic
Alcenter} placeholdertext: "Input words ..." Text: "KFC" onaccepted: {//clear all of the markers Mapitemview
. Model.reset () Searchmodel.searchterm = text searchmodel.update (); }}} Item {anchors {left:parent.left righ T:parent.right Bottom:parent.bottom Top:page.header.bottom} M AP {Id:map ANCHORS.FIll:parent Plugin:plugin {name: "OSM"} center:cur
Location zoomlevel:13 Gesture {onpanfinished: { Centercurrent = False}} mapcircle {Id:my
Location Center:positionSource.position.coordinate Radius:units.gu (4)
Color: "Yellow"} mapitemview {Id:mapitemview
Model:searchmodel Delegate:mapquickitem {Id:mapitem
Coordinate:place.location.coordinate Anchorpoint.x:image.width * 0.5
Anchorpoint.y:image.height Sourceitem:column {image { Width:units.gu (3) Height:width Id:image Source: "Marker.png"} Text {text:title; f Ont.bold:true}} mousearea {anchors.fill: Parent onclicked: {onClicked:PopupUtils.open (pop, Mapitem, {
' Model ': Model}}}}}
component.oncompleted: {zoomlevel = 15}}}}
The source code of the whole project is: Https://github.com/liu-xiao-guo/PlaceSearchModel