利用PlaceSearchModel来在规定的区域里搜寻兴趣点
2016-06-21 08:40
417 查看
在Ubuntu SDK中,它提供了一个PlaceSearchModel接口.通过这个接口,我们可以在规定的区域里搜寻我们需要的地点,比如,KFC, Pizzar,或银行.在今天的教程中,我们来利用这个API来展示如何搜寻我们的兴趣点.
![](http://img.blog.csdn.net/20160621083623920?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20160621083647691?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20160621083754530?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20160621083818240?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
PlaceSearchModel的基本用法:
这里的searchTerm就是我们需要搜索的关键词.searchArea标示的是我们以什么为中心进行搜索.每当有一个新的关键词要进行搜索时,我们可以通过如下的方式来做:
searchModel.searchTerm = text
searchModel.update();
这里的plugin是已经存在于我们的系统的plugin.我们实际上是可以通过如下的方式来得到系统所有的plugin的:
具体的使用方法可以参阅文章"在Ubuntu手机上利用Map API来显示地图并动态显示标记".
当一个地点被成功搜寻后,它返回的数据如下:
![](http://img.blog.csdn.net/20160621083208309?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
我们可以通过这些数据来显示我们得到位置的信息.
基于这些理解,我们做出了我们的例程:
整个项目的源码在:https://github.com/liu-xiao-guo/PlaceSearchModel
PlaceSearchModel的基本用法:
PlaceSearchModel { id: searchModel plugin: myPlugin searchTerm: "KFC" searchArea: QtPositioning.circle(curLocation) Component.onCompleted: update() }
这里的searchTerm就是我们需要搜索的关键词.searchArea标示的是我们以什么为中心进行搜索.每当有一个新的关键词要进行搜索时,我们可以通过如下的方式来做:
searchModel.searchTerm = text
searchModel.update();
这里的plugin是已经存在于我们的系统的plugin.我们实际上是可以通过如下的方式来得到系统所有的plugin的:
Plugin { id: plugin // Set the default one Component.onCompleted: { name = availableServiceProviders[0] } }
具体的使用方法可以参阅文章"在Ubuntu手机上利用Map API来显示地图并动态显示标记".
当一个地点被成功搜寻后,它返回的数据如下:
我们可以通过这些数据来显示我们得到位置的信息.
基于这些理解,我们做出了我们的例程:
Main.qml
import QtQuick 2.4 import Ubuntu.Components 1.3 import Ubuntu.Components.Popups 1.3 import QtLocation 5.3 import QtPositioning 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(60) height: units.gu(85) property bool centerCurrent: true Plugin { id: myPlugin name: "osm" } PositionSource { id: positionSource property variant lastSearchPosition: curLocation active: true updateInterval: 5000 onPositionChanged: { var currentPosition = positionSource.position.coordinate if ( centerCurrent ) { map.center = currentPosition } var distance = currentPosition.distanceTo(lastSearchPosition) // if (distance > 500) { // 500m from last performed pizza search lastSearchPosition = currentPosition searchModel.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.UnknownSearchResult: return "type: UnknownSearchResult" case PlaceSearchModel.PlaceResult: return "type: PlaceResult" case PlaceSearchModel.ProposedSearchResult: return "type: ProposedSearchResult" } } fontSize: "medium" } Label { anchors.horizontalCenter: 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.sponsored fontSize: "medium" } } } } Page { id: page header: standardHeader PageHeader { id: standardHeader visible: page.header === standardHeader title: "PlaceSearchModel" trailingActionBar.actions: [ Action { iconName: "edit" text: "Edit" onTriggered: page.header = editHeader } ] } PageHeader { id: editHeader visible: page.header === editHeader leadingActionBar.actions: [ Action { iconName: "back" text: "Back" onTriggered: { page.header = standardHeader } } ] contents: TextField { id: input anchors { left: parent.left right: parent.right verticalCenter: parent.verticalCenter } placeholderText: "input words .." text: "KFC" onAccepted: { // clear all of the markers mapItemView.model.reset() searchModel.searchTerm = text searchModel.update(); } } } Item { anchors { left: parent.left right: parent.right bottom: parent.bottom top: page.header.bottom } Map { id: map anchors.fill: parent plugin: Plugin { name: "osm" } center: curLocation zoomLevel: 13 gesture { onPanFinished: { centerCurrent = false } } MapCircle { id: mylocation 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; font.bold: true } } MouseArea { anchors.fill: parent onClicked: { onClicked: PopupUtils.open(pop, mapitem, { 'model': model }) } } } } Component.onCompleted: { zoomLevel = 15 } } } } }
整个项目的源码在:https://github.com/liu-xiao-guo/PlaceSearchModel
相关文章推荐
- 第十周项目2-存储班长信息的学生类
- “爱运动管理系统”诞生记(1)
- thinkphp 3.1.2 在视图JS代码中获取控制器中的数组
- Alpha阶段项目总结
- 超接地气的新品操作干货
- Remove Duplicates from Sorted List II
- 开源新闻速递:Solus 1.2 "Shannon" 正式发布
- linux shell 判断文件或文件夹是否真的存在
- Android 实战开发总结(widget篇)
- 简单实现Java web服务器
- 如何通过C#.NET中的反射机制来创建C#.NET泛型类的实例,并调用其方法??
- 四人团-江南行-乌镇东栅旅游
- Boosting算法简介
- Linux中如何使用gThumb批量调整图片大小
- 唯有坚持和努力不可辜负
- 转--《失控》与无中生有
- NSCharacterSet 详解
- 如何让Ubuntu系统支持WebP图片格式
- 决胜笔记本市场,走心是关键
- 正确使用Block避免Cycle Retain和Crash