QT使用QML實(shí)現(xiàn)地圖繪制虛線(xiàn)的示例代碼
QML提供了MapPolyline用于在地圖上繪制線(xiàn)段,該線(xiàn)段是實(shí)線(xiàn),因此我使用Canvas自定義繪制的方式在地圖上繪制線(xiàn)段,如圖:
鼠標(biāo)在地圖上點(diǎn)擊后,在點(diǎn)擊位置添加圖標(biāo) ,當(dāng)有多個(gè)圖標(biāo)被添加到地圖上后,計(jì)算各個(gè)圖標(biāo)間的距離,并創(chuàng)建一個(gè)新的虛線(xiàn)線(xiàn)段組件,連接兩個(gè)圖標(biāo)點(diǎn),顯示距離數(shù)值。如果對(duì)自定義圖標(biāo)添加拖動(dòng)屬性,效果如圖:
MapDashLine.qml屬性:
- beginCoordinate:線(xiàn)段起始經(jīng)緯度坐標(biāo)
- endCoordinate:線(xiàn)段終點(diǎn)經(jīng)緯度坐標(biāo)
- lineDash:虛線(xiàn)樣式
- lineColor:虛線(xiàn)顏色
- lineWidth:虛線(xiàn)粗細(xì)
- 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) //**繪制虛線(xiàn) 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() } }
到此這篇關(guān)于QT使用QML實(shí)現(xiàn)地圖繪制虛線(xiàn)的示例代碼的文章就介紹到這了,更多相關(guān)Qt QML地圖繪制虛線(xiàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Visual Studio 2022中創(chuàng)建的C++項(xiàng)目無(wú)法使用萬(wàn)能頭<bits/stdc++.h>的
如果大家也遇到下面這種問(wèn)題,可能是沒(méi)有include文件夾中沒(méi)有bits/stdc++.h,這篇文章主要介紹了Visual Studio 2022中創(chuàng)建的C++項(xiàng)目無(wú)法使用萬(wàn)能頭<bits/stdc++.h>的解決方案,感興趣的朋友跟隨小編一起看看吧2024-02-02QT通過(guò)C++線(xiàn)程池運(yùn)行Lambda自定義函數(shù)流程詳解
最近在接觸公司的一個(gè)QT桌面項(xiàng)目,其中里面有一個(gè)模塊是使用線(xiàn)程池去運(yùn)行自定義函數(shù)的,自己潛心研究那個(gè)線(xiàn)程池代碼一天,發(fā)現(xiàn)研究不透,看不懂,里面幾乎都是使用C++11的新特性進(jìn)行編寫(xiě)2022-10-10Pipes實(shí)現(xiàn)LeetCode(194.轉(zhuǎn)置文件)
這篇文章主要介紹了Pipes實(shí)現(xiàn)LeetCode(194.轉(zhuǎn)置文件),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08OpenCV圖像特征提取之Shi-Tomasi角點(diǎn)檢測(cè)算法詳解
Harris角點(diǎn)檢測(cè)算法就是對(duì)角點(diǎn)響應(yīng)函數(shù)R進(jìn)行閾值處理,Shi-Tomasi原理幾乎和Harris一樣的,只不過(guò)最后計(jì)算角點(diǎn)響應(yīng)的公式發(fā)生了變化。本文將和大家詳細(xì)說(shuō)說(shuō)Shi-Tomasi角點(diǎn)檢測(cè)算法的原理與實(shí)現(xiàn),需要的可以參考一下2022-09-09利用C++實(shí)現(xiàn)通訊錄管理系統(tǒng)的完整代碼
通訊錄是一個(gè)可以記錄親人、好友信息的工具,下面這篇文章主要給大家介紹了關(guān)于利用C++實(shí)現(xiàn)通訊錄管理系統(tǒng)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06c++獲取sqlite3數(shù)據(jù)庫(kù)表中所有字段的方法小結(jié)
本文給大家分享c++獲取sqlite3數(shù)據(jù)庫(kù)表中所有字段的三種常用方法,本文針對(duì)每一種方法給大家詳細(xì)介紹,需要的的朋友通過(guò)本文一起學(xué)習(xí)吧2016-11-11