QML 地图可拖拽位置标签组件

15 篇文章 35 订阅
10 篇文章 0 订阅

在地图上显示位置信息时,有时候需要同时显示该位置的详细信息。该组件可在地图上显示一个连接到地图地理位置的标签框,该标签框可点击进行拖拽。在地理位置改变、地图缩放、地图平移时,该标签框的相对位置保持不变。(注意:该组件父类必须为Map)如图:

 

属性:

name:组件名,用于查找组件

anchorCoordinate:标签点指向的经纬度

backgroundColor:背景颜色

borderColor:边框颜色

lineColor:连接线颜色

方法:

update():绘制标签

pointsChanged(coordinate):更新标签位置

当需要显示标签时,设置完anchorCoordinate后调用update()函数绘制标签: 

Map {
        id:myMap
        anchors.fill: parent
        plugin: mapPlugin
        center: QtPositioning.coordinate(25.0018,102.9409) 
        zoomLevel: 6
        color: "#00000000"
        copyrightsVisible: false
        activeMapType:supportedMapTypes[1]

        MapPointLabel{
            id:mapPointLabel
            anchors.fill: parent
            name:"test"
            backgroundColor: "#cc115777"
            lineColor:"magenta"
            anchorCoordinate: QtPositioning.coordinate(25.0018,102.9409)
            Component.onCompleted: {
                update()
            }
        }
    }

当需要动态更新标签位置时,调用函数pointsChanged(coordinate)传入最新的经纬度即可,不需要调用update()函数:

Timer{
        id:timer
        interval: 1800
        repeat: true
        running: true
        onTriggered: {
            if(pathIndex<9){
                mapPointLabel.pointsChanged(pathList[pathIndex])//传入经纬度
                pathIndex++
            }else{
                pathIndex = 0
            }
        }
    }

Demo示例GitHub地址:

https://github.com/zjgo007/QmlDemo/tree/master/MapLabelhttps://github.com/zjgo007/QmlDemo/tree/master/MapLabelDemo示例CSDN下载地址:

https://download.csdn.net/download/zjgo007/82888234https://download.csdn.net/download/zjgo007/82888234

组件完整代码  MapPointLabel.qml:

import QtQuick 2.9
import QtPositioning 5.9
import QtLocation 5.9


Item{
    id:mainItem
    anchors.fill: parent
    property string name: ""
    property var anchorCoordinate: QtPositioning.coordinate(0,0)
    property color backgroundColor: "#b3115777"
    property color borderColor: "#e6115777"
    property color lineColor: "floralwhite"

    QtObject{
        id:object
        property point anchorPoint: "0,0"
    }

    Rectangle {
        id:mapPointLabel
        width: 60
        height: 100
        border.color: mainItem.borderColor
        color: mainItem.backgroundColor

        property int offsetX: -mapPointLabel.width/2
        property int offsetY: -mapPointLabel.height/2
        readonly property point center: Qt.point(x+mapPointLabel.width/2,y+mapPointLabel.height/2)
        property bool draged: false


        onXChanged: {
            if(!mapPointLabel.draged)
                return
            offsetX = x-object.anchorPoint.x
            myCanvas.requestPaint()
        }

        onYChanged: {
            if(!mapPointLabel.draged)
                return
            offsetY = y-object.anchorPoint.y
            myCanvas.requestPaint()
        }

        MouseArea{
            anchors.fill: parent
            drag.target: mapPointLabel
            drag.axis: Drag.XAndYAxis
            preventStealing: true
            onPressed: {
                mapPointLabel.draged = true
                mapPointLabel.border.color = "crimson"
            }
            onReleased: {
                mapPointLabel.draged = false
                mapPointLabel.border.color = mainItem.borderColor
            }
        }

    }


    Canvas{
        id:myCanvas
        anchors.fill: parent
        z:-1
        onPaint: {
            var ctx = getContext("2d")
            ctx.clearRect(0,0,myCanvas.width,myCanvas.height)
            ctx.strokeStyle = mainItem.lineColor
            ctx.lineWidth = 2;
            ctx.setLineDash([5,3])
            ctx.beginPath()
            ctx.moveTo(mapPointLabel.center.x,mapPointLabel.center.y)
            ctx.lineTo(object.anchorPoint.x,object.anchorPoint.y)
            ctx.stroke()
            ctx.clearRect(mapPointLabel.x,mapPointLabel.y,mapPointLabel.width,mapPointLabel.height)
        }
    }

    Connections{
        target: mainItem.parent
        function onZoomLevelChanged(){
            update()
        }
        function onVisibleRegionChanged(){
            update()
        }
    }

    function update(){
        object.anchorPoint = parent.fromCoordinate(anchorCoordinate,false)
        mapPointLabel.x = object.anchorPoint.x + mapPointLabel.offsetX
        mapPointLabel.y = object.anchorPoint.y + mapPointLabel.offsetY
        myCanvas.requestPaint()
    }

    function pointsChanged(coordinate){
        anchorCoordinate = coordinate
        update()
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵喵叫的猴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值