系统:Ubuntu
环境:qt
地图插件:mapboxgl
(1)在地图上增加图层,(地图上画点\线\面等跟js接口一致)官网上例子:
Map {
plugin: Plugin { name: "mapboxgl" }
center: QtPositioning.coordinate(60.170448, 24.942046) // Helsinki
zoomLevel: 12
MapParameter {
type: "source"
property var name: "routeSource"
property var sourceType: "geojson"
property var data: '{ "type": "FeatureCollection", "features": \
[{ "type": "Feature", "properties": {}, "geometry": { \
"type": "LineString", "coordinates": [[ 24.934938848018646, \
60.16830257086771 ], [ 24.943315386772156, 60.16227776476442 ]]}}]}'
}
MapParameter {
type: "layer"
property var name: "route"
property var layerType: "line"
property var source: "routeSource"
// Draw under the first road label layer
// of the mapbox-streets style.
property var before: "road-label-small"
}
MapParameter {
type: "paint"
property var layer: "route"
property var lineColor: "blue"
property var lineWidth: 8.0
}
MapParameter {
type: "layout"
property var layer: "route"
property var lineJoin: "round"
property var lineCap: "round"
}
}
注:其中,type是source的数据源data是geojson的格式:因此data也可以写成:
MapParameter {
type: "source"
property var name: "routeSource"
property var sourceType: "geojson
property var data: ":source.geojson"
}
(2)如果想要对数据源进行修改,可以设置id,如:source然后source.data = ":source2.geojson"
MapParameter {
id:source
type: "source"
property var name: "routeSource"
property var sourceType: "geojson
property var data: ":source.geojson"
}
或者
MapParameter {
id:source
type: "source"
property var name: "routeSource"
property var sourceType: "geojson
property var data: '{ "type": "FeatureCollection", "features":[]}'
}
Component.onCompleted:
{
var sourceData = '{ "type": "FeatureCollection", "features":[]}'//这是空值
source.data = sourceData
}
(3)可以通过addMapparameter形式添加:例如:
var param = Qt.createQmlObject('import QtLocation 5.9; MapParameter { \
type: "paint"; \
property var layer: "water"; \
property var fillColor: "red"; \
}', map)
map.addMapParameter(param)
同样的source\layer\paint\layout也是都可以通过该方式添加.