18QT_IMPL_METATYPE_EXTERN(QGeoCoordinate)
20static const double qgeocoordinate_EARTH_MEAN_RADIUS = 6371.0072;
23QGeoCoordinatePrivate::QGeoCoordinatePrivate():
29QGeoCoordinatePrivate::QGeoCoordinatePrivate(
const QGeoCoordinatePrivate &other)
36QGeoCoordinatePrivate::~QGeoCoordinatePrivate()
40QGeoMercatorCoordinatePrivate::QGeoMercatorCoordinatePrivate():
41 QGeoCoordinatePrivate(),
46QGeoMercatorCoordinatePrivate::QGeoMercatorCoordinatePrivate(
const QGeoMercatorCoordinatePrivate &other)
47 : QGeoCoordinatePrivate(other),
48 m_mercatorX(other.m_mercatorX),
49 m_mercatorY(other.m_mercatorY)
52QGeoMercatorCoordinatePrivate::~QGeoMercatorCoordinatePrivate()
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
81
82
83
84
85
86
87
90
91
92
93
94
95
96
97
98
99
100
101
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
140
141
142
143
144
145
146
147
148
149
152
153
154
155
156
157
158
159
160
161
162
165
166
167
168QGeoCoordinate::QGeoCoordinate()
169 : d(
new QGeoCoordinatePrivate)
174
175
176
177
178
179
180
181
182QGeoCoordinate::QGeoCoordinate(
double latitude,
double longitude)
183 : d(
new QGeoCoordinatePrivate)
185 if (QLocationUtils::isValidLat(latitude) && QLocationUtils::isValidLong(longitude)) {
192
193
194
195
196
197
198
199
200
201
202
203QGeoCoordinate::QGeoCoordinate(
double latitude,
double longitude,
double altitude)
204 : d(
new QGeoCoordinatePrivate)
206 if (QLocationUtils::isValidLat(latitude) && QLocationUtils::isValidLong(longitude)) {
214
215
216QGeoCoordinate::QGeoCoordinate(
const QGeoCoordinate &other)
221
222
223
224
225
226
227
228
229
232
233
234QGeoCoordinate &QGeoCoordinate::operator=(
const QGeoCoordinate &other)
244
245
246
247
248
249
250
251
252
253
256
257
258QGeoCoordinate::~QGeoCoordinate()
262QT_DEFINE_QSDP_SPECIALIZATION_DTOR(QGeoCoordinatePrivate)
265
266
267
268
269
270
271
272
275
276
277
278
279
280
283
284
285
286
289
290
291bool QGeoCoordinate::isValid()
const
293 CoordinateType t = type();
294 return t == Coordinate2D || t == Coordinate3D;
298
299
300QGeoCoordinate::CoordinateType QGeoCoordinate::type()
const
302 if (QLocationUtils::isValidLat(d->lat)
303 && QLocationUtils::isValidLong(d->lng)) {
308 return InvalidCoordinate;
313
314
315
316
317
318
319
320
321double QGeoCoordinate::latitude()
const
327
328
329
330
331
332
333
334void QGeoCoordinate::setLatitude(
double latitude)
340
341
342
343
344
345
346
347
348double QGeoCoordinate::longitude()
const
354
355
356
357
358
359
360
361void QGeoCoordinate::setLongitude(
double longitude)
367
368
369
370
371
372
373double QGeoCoordinate::altitude()
const
379
380
381
382
383void QGeoCoordinate::setAltitude(
double altitude)
389
390
391
392
393
394
395
396
397
398
399qreal QGeoCoordinate::distanceTo(
const QGeoCoordinate &other)
const
401 if (type() == QGeoCoordinate::InvalidCoordinate
402 || other.type() == QGeoCoordinate::InvalidCoordinate) {
407 double dlat = qDegreesToRadians(other.d->lat - d->lat);
408 double dlon = qDegreesToRadians(other.d->lng - d->lng);
409 double haversine_dlat = sin(dlat / 2.0);
410 haversine_dlat *= haversine_dlat;
411 double haversine_dlon = sin(dlon / 2.0);
412 haversine_dlon *= haversine_dlon;
413 double y = haversine_dlat
414 + cos(qDegreesToRadians(d->lat))
415 * cos(qDegreesToRadians(other.d->lat))
417 double x = 2 * asin(sqrt(y));
418 return qreal(x * qgeocoordinate_EARTH_MEAN_RADIUS * 1000);
422
423
424
425
426
427
428
429
430
431
432qreal QGeoCoordinate::azimuthTo(
const QGeoCoordinate &other)
const
434 if (type() == QGeoCoordinate::InvalidCoordinate
435 || other.type() == QGeoCoordinate::InvalidCoordinate) {
439 double dlon = qDegreesToRadians(other.d->lng - d->lng);
440 double lat1Rad = qDegreesToRadians(d->lat);
441 double lat2Rad = qDegreesToRadians(other.d->lat);
443 double y = sin(dlon) * cos(lat2Rad);
444 double x = cos(lat1Rad) * sin(lat2Rad) - sin(lat1Rad) * cos(lat2Rad) * cos(dlon);
446 double azimuth = qRadiansToDegrees(atan2(y, x)) + 360.0;
448 double fraction = modf(azimuth, &whole);
449 return qreal((
int(whole + 360) % 360) + fraction);
452void QGeoCoordinatePrivate::atDistanceAndAzimuth(
const QGeoCoordinate &coord,
453 qreal distance, qreal azimuth,
454 double *lon,
double *lat)
456 double latRad = qDegreesToRadians(coord.d->lat);
457 double lonRad = qDegreesToRadians(coord.d->lng);
458 double cosLatRad = cos(latRad);
459 double sinLatRad = sin(latRad);
461 double azimuthRad = qDegreesToRadians(azimuth);
463 double ratio = (distance / (qgeocoordinate_EARTH_MEAN_RADIUS * 1000.0));
464 double cosRatio = cos(ratio);
465 double sinRatio = sin(ratio);
467 double resultLatRad = asin(sinLatRad * cosRatio
468 + cosLatRad * sinRatio * cos(azimuthRad));
469 double resultLonRad = lonRad + atan2(sin(azimuthRad) * sinRatio * cosLatRad,
470 cosRatio - sinLatRad * sin(resultLatRad));
472 *lat = qRadiansToDegrees(resultLatRad);
473 *lon = qRadiansToDegrees(resultLonRad);
477
478
479
480
481
482
483
484
485
486QGeoCoordinate QGeoCoordinate::atDistanceAndAzimuth(qreal distance, qreal azimuth, qreal distanceUp)
const
489 return QGeoCoordinate();
491 double resultLon, resultLat;
492 QGeoCoordinatePrivate::atDistanceAndAzimuth(*
this, distance, azimuth,
493 &resultLon, &resultLat);
494 double resultAlt = d->alt + distanceUp;
495 return QGeoCoordinate(resultLat, QLocationUtils::wrapLong(resultLon), resultAlt);
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533QString QGeoCoordinate::toString(CoordinateFormat format)
const
535 if (type() == QGeoCoordinate::InvalidCoordinate)
541 double absLat = qAbs(d->lat);
542 double absLng = qAbs(d->lng);
546 case DegreesWithHemisphere: {
547 latStr = QString::asprintf(
"%.5f°", absLat);
548 longStr = QString::asprintf(
"%.5f°", absLng);
552 case DegreesMinutesWithHemisphere: {
553 double latMin = (absLat -
int(absLat)) * 60;
554 double lngMin = (absLng -
int(absLng)) * 60;
561 if (latMin > 59.9995) {
565 if (lngMin > 59.9995) {
570 latStr = QString::asprintf(
"%d° %.3f'",
int(absLat), latMin);
571 longStr = QString::asprintf(
"%d° %.3f'",
int(absLng), lngMin);
574 case DegreesMinutesSeconds:
575 case DegreesMinutesSecondsWithHemisphere: {
576 double latMin = (absLat -
int(absLat)) * 60;
577 double lngMin = (absLng -
int(absLng)) * 60;
578 double latSec = (latMin -
int(latMin)) * 60;
579 double lngSec = (lngMin -
int(lngMin)) * 60;
586 if (latSec >= 59.95) {
592 if (qRound(latMin) >= 60) {
597 if (lngSec >= 59.95) {
600 if (qRound(lngMin) >= 60) {
606 latStr = QString::asprintf(
"%d° %d' %.1f\"",
int(absLat),
int(latMin), latSec);
607 longStr = QString::asprintf(
"%d° %d' %.1f\"",
int(absLng),
int(lngMin), lngSec);
616 case DegreesMinutesSeconds: {
618 latStr.insert(0, QStringLiteral(
"-"));
620 longStr.insert(0, QStringLiteral(
"-"));
623 case DegreesWithHemisphere:
624 case DegreesMinutesWithHemisphere:
625 case DegreesMinutesSecondsWithHemisphere: {
627 latStr.append(QString::fromLatin1(
" S"));
629 latStr.append(QString::fromLatin1(
" N"));
631 longStr.append(QString::fromLatin1(
" W"));
633 longStr.append(QString::fromLatin1(
" E"));
639 return QString::fromLatin1(
"%1, %2").arg(latStr, longStr);
640 return QString::fromLatin1(
"%1, %2, %3m").arg(latStr, longStr, QString::number(d->alt));
643bool QGeoCoordinate::equals(
const QGeoCoordinate &lhs,
const QGeoCoordinate &rhs)
645 bool latEqual = (qIsNaN(lhs.d->lat) && qIsNaN(rhs.d->lat))
646 || QtPrivate::fuzzyCompare(lhs.d->lat, rhs.d->lat);
647 bool lngEqual = (qIsNaN(lhs.d->lng) && qIsNaN(rhs.d->lng))
648 || QtPrivate::fuzzyCompare(lhs.d->lng, rhs.d->lng);
649 bool altEqual = (qIsNaN(lhs.d->alt) && qIsNaN(rhs.d->alt))
650 || QtPrivate::fuzzyCompare(lhs.d->alt, rhs.d->alt);
652 if (!qIsNaN(lhs.d->lat) && ((lhs.d->lat == 90.0) || (lhs.d->lat == -90.0)))
655 return (latEqual && lngEqual && altEqual);
658QGeoCoordinate::QGeoCoordinate(QGeoCoordinatePrivate &dd):
663#ifndef QT_NO_DEBUG_STREAM
664QDebug QGeoCoordinate::debugStreaming(QDebug dbg,
const QGeoCoordinate &coord)
666 QDebugStateSaver saver(dbg);
667 double lat = coord.latitude();
668 double lng = coord.longitude();
670 QTextStreamManipulator tsm = qSetRealNumberPrecision(11);
672 dbg.nospace() <<
"QGeoCoordinate(";
682 if (coord.type() == QGeoCoordinate::Coordinate3D) {
684 dbg << coord.altitude();
691#ifndef QT_NO_DATASTREAM
693
694
695
696
697
698
700QDataStream &QGeoCoordinate::dataStreamOut(QDataStream &stream,
const QGeoCoordinate &coordinate)
702 stream << coordinate.latitude();
703 stream << coordinate.longitude();
704 stream << coordinate.altitude();
709#ifndef QT_NO_DATASTREAM
711
712
713
714
715
716
717
719QDataStream &QGeoCoordinate::dataStreamIn(QDataStream &stream, QGeoCoordinate &coordinate)
723 coordinate.setLatitude(value);
725 coordinate.setLongitude(value);
727 coordinate.setAltitude(value);
733
734
735
736
739 QtPrivate::QHashCombine hash(seed);
741 if (coordinate.latitude() != 90.0 && coordinate.latitude() != -90.0)
742 seed = hash(seed, coordinate.longitude());
743 seed = hash(seed, coordinate.latitude());
744 seed = hash(seed, coordinate.altitude());
750#include "moc_qgeocoordinate.cpp"
Combined button and popup list for selecting options.
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept