在QML地图上,我们可以自定义的画圆,画方,画线等等。当我们需要有较多的数据需要在地图上展示时,通过动态新增的方式很明显不利于管理和维护,因此用到MapItemView。
MapItemView用于展示地图控件,数据来源为model(ListModel、RouteModel等)。是不是和ListView很相似,仅仅不同于MapItemView的代理Delegate需要为地图控件,即位置设置为经纬度坐标。ListView的Delegate控件为桌面控件,位置设置为屏幕位置。下面我通过一个简单示例在地图上显示ListModel中存储的位置数据:
我们在Map下添加MapItemView,设置数据源model和代理delegate:
MapItemView{
model:airModel
delegate:airDelegate
}
接下来设置model和delegate,我此处的代理使用了一个图片,目的是地图放大缩小时候图片大小不随缩放而变化,使用例如MapCircle等控件也是可以的
ListModel{
id: airModel
}
Component{
id:airDelegate
MapQuickItem{
id:airLine
width: 6
height: 6
coordinate:QtPositioning.coordinate(location.latitude,location.longitude)
sourceItem: Image{
sourceSize.height: 6
sourceSize.width: 6
source: "Img/signal2.png"
}
anchorPoint.x: 3
anchorPoint.y:3
}
}
最后一步就是往model中写入数据,如果model中已有数据,此步骤可忽略:
Connections{
target: Analyze //C++中获得数据后发送信号
onMonitorChange:{
for(var i=0;i<locationList.length;i++){
var point = locationList[i]
airModel.append({"location":QtPositioning.coordinate(point.x,point.y)})
// if(i==locationList.length-1){//最终目标位置
// airLine.coordinate = QtPositioning.coordinate(point.x,point.y)
// }
}
}
}
效果如图: