From 98bb661aa365a3c63f215a61f9508ade7450b5b6 Mon Sep 17 00:00:00 2001 From: ic005k Date: Tue, 18 Feb 2025 19:22:45 +0800 Subject: [PATCH] map --- src/MainWindow.cpp | 2 ++ src/Steps/Steps.cpp | 49 +++++++++++++++++++++++++++-------- src/Steps/Steps.h | 5 ++-- src/qmlsrc/map.qml | 62 +++++++++++++++++++++++++-------------------- 4 files changed, 78 insertions(+), 40 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index bcc1f20d..f5951962 100755 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -3425,6 +3425,8 @@ void MainWindow::initQW() { ui->qwMap->setSource(QUrl(QStringLiteral("qrc:/src/qmlsrc/map.qml"))); ui->qwMap->rootContext()->setContextProperty("isGpsRun", false); + ui->qwMap->rootContext()->setContextProperty("strDistance", "0 km"); + ui->qwMap->rootContext()->setContextProperty("strSpeed", "0 km/h"); ui->qwReport->rootContext()->setContextProperty("m_Report", m_Report); ui->qwReport->setSource(QUrl(QStringLiteral("qrc:/src/qmlsrc/report.qml"))); diff --git a/src/Steps/Steps.cpp b/src/Steps/Steps.cpp index f0f290f4..21c3a8ba 100755 --- a/src/Steps/Steps.cpp +++ b/src/Steps/Steps.cpp @@ -404,8 +404,10 @@ void Steps::startRecordMotion() { m_activity.callMethod("getTotalDistance", "()D"); QString str_distance = QString::number(distance, 'f', 2); m_distance = str_distance.toDouble(); - latitude = m_activity.callMethod("getLatitude", "()D"); - longitude = m_activity.callMethod("getLongitude", "()D"); + if (!isGpsTest) { + latitude = m_activity.callMethod("getLatitude", "()D"); + longitude = m_activity.callMethod("getLongitude", "()D"); + } QAndroidJniObject jstrGpsStatus = m_activity.callObjectMethod("getGpsStatus"); if (jstrGpsStatus.isValid()) { @@ -425,13 +427,25 @@ void Steps::startRecordMotion() { strGpsStatus = str4 + "\n" + str5 + "\n" + str6 + "\n" + str7; } - if (m_time.second() % 3 && m_distance > 0) { - appendTrack(latitude, longitude); - nWriteGpsCount++; - writeGpsPos(latitude, longitude, nWriteGpsCount, nWriteGpsCount); + if (m_time.second() % 3) { + if (!isGpsTest) { + if (m_distance > 0) { + appendTrack(latitude, longitude); + nWriteGpsCount++; + writeGpsPos(latitude, longitude, nWriteGpsCount, nWriteGpsCount); + } + } else { + appendTrack(latitude, longitude); + latitude = latitude + 0.001; + longitude = longitude + 0.001; + nWriteGpsCount++; + writeGpsPos(latitude, longitude, nWriteGpsCount, nWriteGpsCount); + } + + mw_one->ui->qwMap->rootContext()->setContextProperty("strDistance", + str1); + mw_one->ui->qwMap->rootContext()->setContextProperty("strSpeed", str3); } - mw_one->ui->qwMap->rootContext()->setContextProperty("strDistance", str1); - mw_one->ui->qwMap->rootContext()->setContextProperty("strSpeed", str3); } #else @@ -439,6 +453,17 @@ void Steps::startRecordMotion() { m_speed = m_distance / m_time.second(); emit speedChanged(); } + + if (isGpsTest) { + if (m_time.second() % 3) { + appendTrack(latitude, longitude); + latitude = latitude + 0.001; + longitude = longitude + 0.001; + nWriteGpsCount++; + writeGpsPos(latitude, longitude, nWriteGpsCount, nWriteGpsCount); + } + } + #endif strTotalDistance = QString::number(m_TotalDistance) + " km"; @@ -507,8 +532,10 @@ void Steps::positionUpdated(const QGeoPositionInfo& info) { } lastPosition = info.coordinate(); - latitude = lastPosition.latitude(); - longitude = lastPosition.longitude(); + if (!isGpsTest) { + latitude = lastPosition.latitude(); + longitude = lastPosition.longitude(); + } } void Steps::stopRecordMotion() { @@ -760,7 +787,7 @@ void Steps::writeGpsPos(double lat, double lon, int i, int count) { QString s0 = t0.replace(" ", ""); QString s1 = strStartTime.replace(":", ""); - QSettings Reg(iniDir + s0 + "-gps-" + s1, QSettings::IniFormat); + QSettings Reg(iniDir + s0 + "-gps-" + s1 + ".ini", QSettings::IniFormat); #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) Reg.setIniCodec("utf-8"); #endif diff --git a/src/Steps/Steps.h b/src/Steps/Steps.h index b7b999c4..4e0c76d5 100644 --- a/src/Steps/Steps.h +++ b/src/Steps/Steps.h @@ -99,8 +99,8 @@ class Steps : public QDialog { QTimer *timer; QString strTotalDistance; QString strDurationTime; - double latitude; - double longitude; + double latitude = 59.91; + double longitude = 10.75; QString strGpsStatus; QString strGpsInfoShow; QString lblStyle; @@ -121,6 +121,7 @@ class Steps : public QDialog { void clearTrack(); void writeGpsPos(double lat, double lon, int i, int count); int nWriteGpsCount; + bool isGpsTest = false; signals: void distanceChanged(double distance); void timeChanged(); diff --git a/src/qmlsrc/map.qml b/src/qmlsrc/map.qml index 63fb8d04..cf8aa8ba 100644 --- a/src/qmlsrc/map.qml +++ b/src/qmlsrc/map.qml @@ -9,31 +9,27 @@ Rectangle { height: 600 // 变量定义 - property double gpsx: 51.5074 - property double gpsy: -0.1278 + property double gpsx: 59.91 + property double gpsy: 10.75 - //property string strSpeed: "0.00" - // property string strDistance: "0.00" function appendTrack(lat, lon) { - - // trajectory.path.push({ - // "latitude": lat, - // "longitude": lon - // }) - var newPath = [{ - "latitude": lat, - "longitude": lon - } // London - // Paris - ] - trajectory.path = newPath // 使用新的路径更新 gpsx = lat gpsy = lon - map.center = QtPositioning.coordinate(gpsx, gpsy) + + var newCoordinate = QtPositioning.coordinate(lat, lon) + + //polyline.path.push([newCoordinate]) + + + let pathArray = polyline.path + + pathArray.push(newCoordinate); // 添加新的点 + polyline.path = pathArray + + map.center = newCoordinate } - function clearTrack() { - trajectory.path = [] + function clearTrack() {polyline.path = [] } Plugin { @@ -46,18 +42,30 @@ Rectangle { anchors.fill: parent plugin: mapPlugin center: QtPositioning.coordinate(gpsx, gpsy) // 初始中心坐标(伦敦) - zoomLevel: 14 + zoomLevel: 13 MapPolyline { - id: trajectory + id: polyline line.color: "red" line.width: 3 - path: [// 初始轨迹点 - QtPositioning.coordinate(51.5074, - -0.1278), QtPositioning.coordinate( - 51.5080, -0.1280), QtPositioning.coordinate( - 51.507, -0.1279), QtPositioning.coordinate(51.5089, - -0.1289)] + //path: [QtPositioning.coordinate(59.91, + // 10.75), + // QtPositioning.coordinate( + // gpsx+0.0000, gpsy+0.0000)//QtPositioning.coordinate(59.912, 10.752) + // ] + } + + // 可选:添加一个标记来表示当前位置 + MapQuickItem { + coordinate: QtPositioning.coordinate(gpsx, gpsy) + anchorPoint.x: markerImage.width/2+6 + anchorPoint.y: markerImage.height/2 + sourceItem: Image { + id: markerImage + source: "/res/marker.png" // 替换为你的标记图标 + width: 32 + height: 32 + } } }