-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMapView.qml
More file actions
174 lines (147 loc) · 6.11 KB
/
MapView.qml
File metadata and controls
174 lines (147 loc) · 6.11 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import QtQuick 2.15
import QtLocation 5.15
import QtPositioning 5.15
import QtQuick.Controls 2.15
Rectangle {
width: 800
height: 600
anchors.fill: parent
Map {
id: map
objectName: "map"
anchors.fill: parent
activeMapType: map.supportedMapTypes[1]
center: QtPositioning.coordinate(61.27136, 73.33983) // Сургут
zoomLevel: 2
minimumZoomLevel: 2
maximumZoomLevel: 20
plugin: Plugin {
name: "osm";
allowExperimental: true
PluginParameter {
name: "osm.mapping.providersrepository.disabled";
value: true
}
PluginParameter {
name: "osm.mapping.offline.directory";
value: ":/offline_tiles/"
}
}
property var markerMap: ({})
function addMarker(id, lat, lon, label, iconSource) {
var component = Qt.createComponent("qrc:/MapMarker.qml");
if (component.status === Component.Ready) {
var marker = component.createObject(map, {
idStr: id,
coordinate: QtPositioning.coordinate(lat, lon),
label: label,
iconSource: iconSource
});
markerMap[id] = marker;
map.addMapItem(marker);
}
}
function removeMarker(id) {
if (markerMap[id]) {
map.removeMapItem(markerMap[id]);
markerMap[id].destroy();
delete markerMap[id];
}
}
function clearMarkers() {
markerMap = []
map.clearMapItems()
}
function haversineDistance(coord1, coord2) {
const R = 6371e3; // Радиус Земли в метрах
const lat1 = coord1.latitude * Math.PI / 180;
const lat2 = coord2.latitude * Math.PI / 180;
const deltaLat = (coord2.latitude - coord1.latitude) * Math.PI / 180;
const deltaLon = (coord2.longitude - coord1.longitude) * Math.PI / 180;
const a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
Math.cos(lat1) * Math.cos(lat2) *
Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const distance = R * c; // расстояние в метрах
const distanceInKilometers = distance / 1000; // перевод в километры
const azimuth = calculateAzimuth(coord1.latitude, coord1.longitude, coord2.latitude, coord2.longitude);
mapController.receiveAzimuth(azimuth);
mapController.receiveCallLatLon(coord2);
return distanceInKilometers;
}
function calculateTotalDistance(path) {
let totalDistance = 0;
for (let i = 0; i < path.length - 1; i++) {
totalDistance += haversineDistance(path[i], path[i + 1]);
}
return totalDistance; // Возвращается сумма расстояний в метрах
}
function addLine(path, color, width) {
if (path.length < 2) return;
let geoPath = [];
for (let i = 0; i < path.length - 1; i++) {
let segment = greatCirclePath(
path[i],
path[i + 1],
64 // чем больше — тем плавнее дуга
);
if (i > 0) segment.shift(); // убираем дублирующуюся точку
geoPath = geoPath.concat(segment);
}
var polyline = Qt.createQmlObject(
'import QtLocation 5.15; MapPolyline {}',
map
);
polyline.path = geoPath;
polyline.line.color = color;
polyline.line.width = width;
map.addMapItem(polyline);
const totalDistance = Math.round(calculateTotalDistance(path));
mapController.receiveDistance(totalDistance);
}
function calculateAzimuth(lat1, lon1, lat2, lon2) {
var toRadians = Math.PI / 180;
var toDegrees = 180 / Math.PI;
var f1 = lat1 * toRadians;
var f2 = lat2 * toRadians;
var deltaLambda = (lon2 - lon1) * toRadians;
var y = Math.sin(deltaLambda) * Math.cos(f2);
var x = Math.cos(f1) * Math.sin(f2) - Math.sin(f1) * Math.cos(f2) * Math.cos(deltaLambda);
var theta = Math.atan2(y, x);
var azimuth = (theta * toDegrees + 360) % 360;
return azimuth;
}
function greatCirclePath(coord1, coord2, segments) {
let path = [];
let lat1 = coord1.latitude * Math.PI / 180;
let lon1 = coord1.longitude * Math.PI / 180;
let lat2 = coord2.latitude * Math.PI / 180;
let lon2 = coord2.longitude * Math.PI / 180;
let d = 2 * Math.asin(Math.sqrt(
Math.pow(Math.sin((lat2 - lat1) / 2), 2) +
Math.cos(lat1) * Math.cos(lat2) *
Math.pow(Math.sin((lon2 - lon1) / 2), 2)
));
if (d === 0) {
return [coord1, coord2];
}
for (let i = 0; i <= segments; i++) {
let f = i / segments;
let A = Math.sin((1 - f) * d) / Math.sin(d);
let B = Math.sin(f * d) / Math.sin(d);
let x = A * Math.cos(lat1) * Math.cos(lon1) +
B * Math.cos(lat2) * Math.cos(lon2);
let y = A * Math.cos(lat1) * Math.sin(lon1) +
B * Math.cos(lat2) * Math.sin(lon2);
let z = A * Math.sin(lat1) + B * Math.sin(lat2);
let lat = Math.atan2(z, Math.sqrt(x * x + y * y));
let lon = Math.atan2(y, x);
path.push(QtPositioning.coordinate(
lat * 180 / Math.PI,
lon * 180 / Math.PI
));
}
return path;
}
}
}