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