使用Qt/C++實現(xiàn)WGS84,高德GCJ-02與百度BD-09坐標系間相互轉化
在做地圖相關開發(fā)時候,繞不開不同坐標系間的轉化,因此我根據(jù)查閱相關資料后將不同坐標系間的轉換封裝到一個GeoTranslate類中,該類轉換函數(shù)不僅支持Qt/C++調用,同時可在QML中直接調用,配合上QML/Map很方便,我將該類做了個Demo,方便使用者使用,效果如圖:
在QML的地圖Map中使用高德的路徑規(guī)劃的效果:
使用方法為將 GeoTranslate類添加到工程中,調用轉換函數(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
調用方法:
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坐標系間相互轉化的詳細內容,更多關于Qt坐標系轉化的資料請關注腳本之家其它相關文章!
相關文章
C++類與對象深入之引用與內聯(lián)函數(shù)與auto關鍵字及for循環(huán)詳解
朋友們好,這篇播客我們繼續(xù)C++的初階學習,現(xiàn)在對一些C++的入門知識做了些總結,整理出來一篇博客供我們一起復習和學習,如果文章中有理解不當?shù)牡胤?還希望朋友們在評論區(qū)指出,我們相互學習,共同進步2022-06-06詳解C++虛函數(shù)中多態(tài)性的實現(xiàn)原理
C++是一種面向對象的編程語言,在C++中,虛函數(shù)是實現(xiàn)多態(tài)性的關鍵。本文就來探討一下C++虛函數(shù)中多態(tài)性的實現(xiàn)原理及其在面向對象編程中的應用吧2023-05-05