MapItemGroup QML Type

MapItemGroup 类型是地图项目的容器。更多

Import Statement: import QtLocation 6.9
Since: QtLocation 5.9

详细说明

MapItemGroup 是 QML 地图插件(QML Maps Plugin)的一部分,其目的是通过使用包含相互关联的地图元素的 qml 文件以及相关绑定,实现代码模块化。

注: 随 Qt 5.9 发布的此 API 为技术预览版。

使用示例

下面的代码段展示了如何使用 MapItemGroup 创建一个MapCircle ,以坐标(63, -18)为中心,半径为 100 公里,用红色填充,周围有绿色边框,两者都包含在一个半透明的蓝色圆圈中,圆圈上有一个可移动整个组的MouseArea 。该组在名为 PolygonGroup.qml 的单独文件中定义:

import QtQuick
import QtPositioning
import QtLocation

MapItemGroup {
    id: itemGroup
    property alias position: mainCircle.center
    property var radius: 100 * 1000
    property var borderHeightPct : 0.3

    MapCircle {
        id: mainCircle
        center : QtPositioning.coordinate(40, 0)
        radius: itemGroup.radius * (1.0 + borderHeightPct)
        opacity: 0.05
        visible: true
        color: 'blue'

        MouseArea{
            anchors.fill: parent
            drag.target: parent
            id: maItemGroup
        }
    }

    MapCircle {
        id: groupCircle
        center: itemGroup.position
        radius: itemGroup.radius
        color: 'crimson'

        onCenterChanged: {
            groupPolyline.populateBorder();
        }
    }

    MapPolyline {
        id: groupPolyline
        line.color: 'green'
        line.width: 3

        function populateBorder() {
            groupPolyline.path = [] // clearing the path
            var waveLength = 8.0;
            var waveAmplitude = groupCircle.radius * borderHeightPct;
            for (var i=0; i <= 360; i++) {
                var wavePhase = (i/360.0 * 2.0 * Math.PI )* waveLength
                var waveHeight = (Math.cos(wavePhase) + 1.0) / 2.0
                groupPolyline.addCoordinate(groupCircle.center.atDistanceAndAzimuth(groupCircle.radius + waveAmplitude * waveHeight , i))
            }
        }

        Component.onCompleted: {
            populateBorder()
        }
    }
}

PolygonGroup.qml 现在是一个可重复使用的组件,可以在地图中用作:

Map {
    id: map
    PolygonGroup {
        id: polygonGroup
        position: QtPositioning.coordinate(63,-18)
    }
}

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.