-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathmainwindow.cpp
100 lines (83 loc) · 2.86 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/***************************************************************************
* QGeoView is a Qt / C ++ widget for visualizing geographic data.
* Copyright (C) 2018-2024 Andrey Yaroshenko.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see https://www.gnu.org/licenses.
****************************************************************************/
#include "mainwindow.h"
#include <QRandomGenerator>
#include <QTimer>
#include <helpers.h>
#include <placemarkcircle.h>
#include <QGeoView/QGVLayerOSM.h>
MainWindow::MainWindow()
{
setWindowTitle("QGeoView Samples - moving objects");
mMap = new QGVMap(this);
setCentralWidget(mMap);
Helpers::setupCachedNetworkAccessManager(this);
// Background layer
auto osmLayer = new QGVLayerOSM();
mMap->addItem(osmLayer);
// Custom layer
auto customLayer = new QGVLayer();
mMap->addItem(customLayer);
// Add moving item in custom layer
auto item = new PlacemarkCircle(QGV::GeoPos(10, 20), 30, Qt::red);
customLayer->addItem(item);
// Moving timer
mTimer = new QTimer();
mTimer->setInterval(100);
mTimer->setSingleShot(false);
connect(mTimer, &QTimer::timeout, this, [this, item]() { moveObject(item); });
mTimer->start();
// Show whole world
QTimer::singleShot(100, this, [this]() {
auto target = mMap->getProjection()->boundaryGeoRect();
mMap->cameraTo(QGVCameraActions(mMap).scaleTo(target));
});
}
MainWindow::~MainWindow()
{
}
void MainWindow::moveObject(PlacemarkCircle* item)
{
static double deltaLat = 0;
static double deltaLon = 0;
const QGV::GeoPos curPos = item->getCenter();
if (curPos.latitude() > 50) {
deltaLat = -1;
deltaLon = 0;
}
if (curPos.latitude() < -50) {
deltaLat = 1;
deltaLon = 0;
}
if (curPos.longitude() > 50) {
deltaLat = 0;
deltaLon = -1;
}
if (curPos.longitude() < -50) {
deltaLat = 0;
deltaLon = +1;
}
if (deltaLat == 0) {
deltaLat = QRandomGenerator::global()->generate() % 2 == 0 ? 2 : -2;
}
if (deltaLon == 0) {
deltaLon = QRandomGenerator::global()->generate() % 2 == 0 ? 2 : -2;
}
const QGV::GeoPos newPos = QGV::GeoPos(curPos.latitude() + deltaLat, curPos.longitude() + deltaLon);
item->setCenter(newPos);
}