使用Qt/C++實現(xiàn)WGS84,高德GCJ-02與百度BD-09坐標系間相互轉(zhuǎn)化
在做地圖相關(guān)開發(fā)時候,繞不開不同坐標系間的轉(zhuǎn)化,因此我根據(jù)查閱相關(guān)資料后將不同坐標系間的轉(zhuǎn)換封裝到一個GeoTranslate類中,該類轉(zhuǎn)換函數(shù)不僅支持Qt/C++調(diào)用,同時可在QML中直接調(diào)用,配合上QML/Map很方便,我將該類做了個Demo,方便使用者使用,效果如圖:

在QML的地圖Map中使用高德的路徑規(guī)劃的效果:

使用方法為將 GeoTranslate類添加到工程中,調(diào)用轉(zhuǎn)換函數(shù)即可
geotranslate.h:
#ifndef GEOTRANSLATE_H
#define GEOTRANSLATE_H
#include <QtMath>
#include <QObject>
#include <QGeoCoordinate>
class GeoTranslate : public QObject
{
public:
explicit GeoTranslate(QObject *parent = nullptr);
static constexpr double pi = 3.14159265358979323846;
static constexpr double a = 6378245.0;
static constexpr double ee = 0.00669342162296594323;
Q_INVOKABLE static QGeoCoordinate wgs84ToGcj02(QGeoCoordinate coordinate);
Q_INVOKABLE static QGeoCoordinate gcj02ToWgs84(QGeoCoordinate coordinate);
Q_INVOKABLE static QGeoCoordinate wgs84ToGcj02(double lat,double lon);
Q_INVOKABLE static QGeoCoordinate gcj02ToWgs84(double lat,double lon);
Q_INVOKABLE static QGeoCoordinate gcj02ToBd09(QGeoCoordinate coordinate);
Q_INVOKABLE static QGeoCoordinate bd09ToGcj02(QGeoCoordinate coordinate);
Q_INVOKABLE static QGeoCoordinate gcj02ToBd09(double gg_lat, double gg_lon);
Q_INVOKABLE static QGeoCoordinate bd09ToGcj02(double bd_lat,double bd_lon);
private:
static double transformLat(double x,double y);
static double transformLon(double x,double y);
static bool outOfChina(double lat,double lon);
static QGeoCoordinate transform(double lat,double lon);
};
#endif // GEOTRANSLATE_H調(diào)用方法:
void Widget::on_pushButton_1_clicked()
{
QGeoCoordinate wgs(ui->lineEditLa_1->text().toDouble(),ui->lineEditLo_1->text().toDouble());
QGeoCoordinate gcj02 = GeoTranslate::wgs84ToGcj02(wgs);
ui->lineEditLa_2->setText(QString::number(gcj02.latitude()));
ui->lineEditLo_2->setText(QString::number(gcj02.longitude()));
}
void Widget::on_pushButton_2_clicked()
{
QGeoCoordinate gcj02(ui->lineEditLa_3->text().toDouble(),ui->lineEditLo_3->text().toDouble());
QGeoCoordinate wgs = GeoTranslate::gcj02ToWgs84(gcj02);
ui->lineEditLa_4->setText(QString::number(wgs.latitude()));
ui->lineEditLo_4->setText(QString::number(wgs.longitude()));
}
void Widget::on_pushButton_3_clicked()
{
QGeoCoordinate gcj02(ui->lineEditLa_5->text().toDouble(),ui->lineEditLo_5->text().toDouble());
QGeoCoordinate bd09 = GeoTranslate::gcj02ToBd09(gcj02);
ui->lineEditLa_6->setText(QString::number(bd09.latitude()));
ui->lineEditLo_6->setText(QString::number(bd09.longitude()));
}
void Widget::on_pushButton_4_clicked()
{
QGeoCoordinate bd09(ui->lineEditLa_7->text().toDouble(),ui->lineEditLo_7->text().toDouble());
QGeoCoordinate gcj02 = GeoTranslate::bd09ToGcj02(bd09);
ui->lineEditLa_8->setText(QString::number(gcj02.latitude()));
ui->lineEditLo_8->setText(QString::number(gcj02.longitude()));
}
以上就是使用Qt/C++實現(xiàn)WGS84,高德GCJ-02與百度BD-09坐標系間相互轉(zhuǎn)化的詳細內(nèi)容,更多關(guān)于Qt坐標系轉(zhuǎn)化的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++類與對象深入之引用與內(nèi)聯(lián)函數(shù)與auto關(guān)鍵字及for循環(huán)詳解
朋友們好,這篇播客我們繼續(xù)C++的初階學(xué)習(xí),現(xiàn)在對一些C++的入門知識做了些總結(jié),整理出來一篇博客供我們一起復(fù)習(xí)和學(xué)習(xí),如果文章中有理解不當?shù)牡胤?還希望朋友們在評論區(qū)指出,我們相互學(xué)習(xí),共同進步2022-06-06
詳解C++虛函數(shù)中多態(tài)性的實現(xiàn)原理
C++是一種面向?qū)ο蟮木幊陶Z言,在C++中,虛函數(shù)是實現(xiàn)多態(tài)性的關(guān)鍵。本文就來探討一下C++虛函數(shù)中多態(tài)性的實現(xiàn)原理及其在面向?qū)ο缶幊讨械膽?yīng)用吧2023-05-05
c++10進制轉(zhuǎn)換為任意2-16進制數(shù)字的實例
下面小編就為大家?guī)硪黄猚++10進制轉(zhuǎn)換為任意2-16進制數(shù)字的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06

