首页 > 其他分享 >QT使用QML实现地图绘制虚线

QT使用QML实现地图绘制虚线

时间:2023-08-15 22:31:55浏览次数:31  
标签:QT beginCoordinate mapDashLine ctx update 虚线 QML var property

QML提供了MapPolyline用于在地图上绘制线段,该线段是实线,因此我使用Canvas自定义绘制的方式在地图上绘制线段,如图:

QT使用QML实现地图绘制虚线_Math

鼠标在地图上点击后,在点击位置添加图标 ,当有多个图标被添加到地图上后,计算各个图标间的距离,并创建一个新的虚线线段组件,连接两个图标点,显示距离数值。如果对自定义图标添加拖动属性,效果如图:

QT使用QML实现地图绘制虚线_自定义_02

MapDashLine.qml属性:

  • beginCoordinate:线段起始经纬度坐标
  • endCoordinate:线段终点经纬度坐标
  • lineDash:虚线样式
  • lineColor:虚线颜色
  • lineWidth:虚线粗细
  • textColor:显示距离文字颜色
  • textPixelSize:字体大小

MapDashLine.qml源码(我使用的是Qt5.15):

import QtQuick 2.15
import QtPositioning 5.15
 
Item {
    id:mapDashLine
    anchors.fill: parent
    property var beginCoordinate: QtPositioning.coordinate()
    property var endCoordinate: QtPositioning.coordinate()
    property var lineDash: [4,4]
    property color lineColor: "crimson"
    property int lineWidth: 2
    property color textColor: "crimson"
    property int textPixelSize: 14
    readonly property var mapItem: mapDashLine.parent
 
 
    Canvas{
        id:myCanvas
        anchors.fill: parent
        onPaint: {
            if(!mapDashLine.beginCoordinate.isValid || !mapDashLine.endCoordinate.isValid)
                return
            var ctx = getContext("2d")
            ctx.clearRect(0,0,myCanvas.width,myCanvas.height)
            ctx.strokeStyle = mapDashLine.lineColor
            ctx.lineWidth = mapDashLine.lineWidth
            ctx.setLineDash(mapDashLine.lineDash)
            //**绘制虚线
            ctx.beginPath()
            var beginPos = mapDashLine.mapItem.fromCoordinate(mapDashLine.beginCoordinate,false)
            ctx.moveTo(beginPos.x,beginPos.y)
            var endPos = mapDashLine.mapItem.fromCoordinate(mapDashLine.endCoordinate,false)
            ctx.lineTo(endPos.x,endPos.y)
            ctx.stroke()
            ctx.save()
            //**绘制文字
            var azimuth = endCoordinate.azimuthTo(beginCoordinate)
            if(azimuth>=180)
                azimuth = azimuth - 180
            var distance = endCoordinate.distanceTo(beginCoordinate)
            var text = (distance/1000).toFixed(0)+"Km"
            ctx.fillStyle = mapDashLine.textColor
            ctx.font = mapDashLine.textPixelSize+"px Arial"
            ctx.textAlign = "center"
            var centerX =  (beginPos.x+endPos.x)/2
            var centerY = (beginPos.y+endPos.y)/2
            ctx.translate(centerX,centerY)
            ctx.rotate(azimuth*Math.PI/180-Math.PI/2)
            ctx.fillText(text,0,-mapDashLine.textPixelSize/2)
            ctx.restore()
        }
    }
 
    onBeginCoordinateChanged: {
        update()
    }
    onEndCoordinateChanged: {
        update()
    }
    onLineDashChanged: {
        update()
    }
    onLineColorChanged: {
        update()
    }
    onLineWidthChanged: {
        update()
    }
    onTextColorChanged: {
        update()
    }
    onTextPixelSizeChanged: {
        update()
    }
 
    Connections{
        target: mapDashLine.mapItem
        function onZoomLevelChanged(){
            update()
        }
        function onVisibleRegionChanged(){
            update()
        }
    }
 
    function update(){
        myCanvas.requestPaint()
    }
}

标签:QT,beginCoordinate,mapDashLine,ctx,update,虚线,QML,var,property
From: https://blog.51cto.com/u_15641375/7094820

相关文章

  • QT:打印当前文件名,行号和函数名称
    定义:#include<QDebug>#definemyDebug()qDebug()<<tr("文件名:")<<__FILE__<<tr("行号:")<<__LINE__<<tr("函数名:")<<__FUNCTION__;在函数中执行myDebug()即可:voidMainWindow::doAction......
  • QT快捷键
    常用高频快捷键Ctrl+/ 多行注释/取消多行注释Ctrl+B 编译工程Ctrl+R 运行工程Ctrl+Alt+up(向上箭头) 当前行向上复制Ctrl+Alt+down(向下箭头) 当前行向下复制Ctrl+Shift+up(向上箭头) 整行向上移动Ctrl+Shift+down(向下箭头) 整行向下移动Ctrl+I 代码格式化对......
  • 基于花生壳和EMQX搭建本地mqtt服务
    本篇关于搭建本地mqtt调试服务器步骤1:下载相关工具,花生壳以及EMQX开源mqtt服务EMqx下载地址:https://www.emqx.com/zh/try?product=broker花生壳下载地址:https://hsk.oray.com/步骤2:花生壳实现内网穿透在这里不做讲解,主要讲述EMQX相关步骤;emqx开源版本下载,解压后,成功后的界面:浏览......
  • ThingsKit物联网平台设备MQTT接入
    入门介绍MQTT基础知识MQTT全称MessageQueuingTelemetryTransport,它是一种基于消息队列的轻量级应用层通信协议,实现了消息发布和订阅。设备可以作为客户端的形式通过它来发布和接收消息,实现数据上报和实时控制。设计用于具有低带宽的受限设备。因此,它是物联网设备接入的完美解......
  • ThingsKit物联网平台网关MQTT接入
    入门介绍在物联网中,网关的作用是将那些本身不能直接连接平台的设备,通过网关的中转,让设备接入平台。网关起到的作用是数据转发和协议转换。网关和平台的通信主要分为:网关设备自身和平台的通信,例如:上报网关自身的设备状态和属性,接收平台对网关的控制指令等。网关子设备和平台的......
  • QML NextQT
    2017年我写了一篇QML从入门到放弃如今2023年,过去6年了。QML是我认为真正革命GUI技术拥抱QML,拥抱声明式GUI.QWidget彻底成为过去式了1.PropertyBinding比如画布有2个盒子AB,A盒子变化大小,B盒子跟着一起干,这个在Qt就直接绑定完事Rectangle{id:Awidth:200heig......
  • 《新建篇》创建Qt项目
    创建Qt项目参考链接:https://blog.csdn.net/weixin_44064908/article/details/131155691法一根据模板创建打开QtCreator界面选择NewProject或者选择菜单栏【文件】-【新建文件或项目】菜单项弹出NewProject对话框,选择QtWidgetsApplication选择【Choose】按钮,弹出如......
  • 《安装篇》下载QT和安装
    Qt下载(多种下载通道+所有版本)参考链接:http://c.biancheng.net/view/3851.html作为Qt下载教程,本文会同时讲解以上三种下载方式。Qt官方下载(非常慢)Qt官网有一个专门的资源下载网站,所有的开发环境和相关工具都可以从这里下载,具体地址是:http://download.qt.io/官网图片:对目......
  • 《简介篇》QT是什么
    参考链接:https://blog.csdn.net/m0_65682542/article/details/126731164QT是什么Qt:一个跨平台的C++开发库,主要用来开发图形用户界面程序。名字含义名字含义:字母Q作为所有类的前缀,是因为Haarard写这个字母看起来特别的漂亮,字母t代表"toolkit",在Xt,Xtoolkit等中得到灵感。......
  • QT画图
    重点:每个QWidget都自带的功能,继承了QPainteDevice都可以使用QPainter来进行绘图。我们需要调用paintEvent绘制事件,这个事件QWidget这个类自带的事件。重写这个事件:如果父类有某个办法,此时这个子类需要有一个更跟父类同样方法名称的。实例化对象写法:QQPainterpainter(this);//......