使用Qt/C++實(shí)現(xiàn)WGS84,高德GCJ-02與百度BD-09坐標(biāo)系間相互轉(zhuǎn)化
在做地圖相關(guān)開發(fā)時(shí)候,繞不開不同坐標(biāo)系間的轉(zhuǎn)化,因此我根據(jù)查閱相關(guān)資料后將不同坐標(biāo)系間的轉(zhuǎn)換封裝到一個(gè)GeoTranslate類中,該類轉(zhuǎn)換函數(shù)不僅支持Qt/C++調(diào)用,同時(shí)可在QML中直接調(diào)用,配合上QML/Map很方便,我將該類做了個(gè)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++實(shí)現(xiàn)WGS84,高德GCJ-02與百度BD-09坐標(biāo)系間相互轉(zhuǎn)化的詳細(xì)內(nèi)容,更多關(guān)于Qt坐標(biāo)系轉(zhuǎn)化的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++類與對(duì)象深入之引用與內(nèi)聯(lián)函數(shù)與auto關(guān)鍵字及for循環(huán)詳解
朋友們好,這篇播客我們繼續(xù)C++的初階學(xué)習(xí),現(xiàn)在對(duì)一些C++的入門知識(shí)做了些總結(jié),整理出來一篇博客供我們一起復(fù)習(xí)和學(xué)習(xí),如果文章中有理解不當(dāng)?shù)牡胤?還希望朋友們在評(píng)論區(qū)指出,我們相互學(xué)習(xí),共同進(jìn)步2022-06-06詳解C++虛函數(shù)中多態(tài)性的實(shí)現(xiàn)原理
C++是一種面向?qū)ο蟮木幊陶Z言,在C++中,虛函數(shù)是實(shí)現(xiàn)多態(tài)性的關(guān)鍵。本文就來探討一下C++虛函數(shù)中多態(tài)性的實(shí)現(xiàn)原理及其在面向?qū)ο缶幊讨械膽?yīng)用吧2023-05-05c++10進(jìn)制轉(zhuǎn)換為任意2-16進(jìn)制數(shù)字的實(shí)例
下面小編就為大家?guī)硪黄猚++10進(jìn)制轉(zhuǎn)換為任意2-16進(jìn)制數(shù)字的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06C語言深入探究動(dòng)態(tài)規(guī)劃之線性DP
線性動(dòng)態(tài)規(guī)劃,是較常見的一類動(dòng)態(tài)規(guī)劃問題,其是在線性結(jié)構(gòu)上進(jìn)行狀態(tài)轉(zhuǎn)移,這類問題不像背包問題、區(qū)間DP等有固定的模板,線性動(dòng)態(tài)規(guī)劃的目標(biāo)函數(shù)為特定變量的線性函數(shù),約束是這些變量的線性不等式或等式,目的是求目標(biāo)函數(shù)的最大值或最小值2022-04-04