溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++怎么實(shí)現(xiàn)百度坐標(biāo)及GCJ02與WGS84之間的轉(zhuǎn)換

發(fā)布時間:2023-03-09 14:36:41 來源:億速云 閱讀:135 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“C++怎么實(shí)現(xiàn)百度坐標(biāo)及GCJ02與WGS84之間的轉(zhuǎn)換”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“C++怎么實(shí)現(xiàn)百度坐標(biāo)及GCJ02與WGS84之間的轉(zhuǎn)換”吧!

實(shí)現(xiàn)代碼

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <math.h>
typedef struct _POSITION
{
    double longitude;
    double latitude;
}POSITION;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private slots:
    double translate_lon(double lon,double lat);
    double translate_lat(double lon,double lat);
    bool outof_China(double lon,double lat);
    POSITION bd09togcj02(double bd_lon, double bd_lat);
    POSITION gcj02tobd09(double gcj_lon,double gcj_lat);
    POSITION gcj02towgs84(double gcj_lon,double gcj_lat);
    POSITION wgs84togcj02(double wgs_lon,double wgs_lat);
    void pushbutton1();
    void pushbutton2();
    void pushbutton3();
private:
    Ui::MainWindow *ui;
    POSITION bd_pos;
    POSITION gcj_pos;
    POSITION wgs_pos;
    double x_PI = 3.14159265358979323846 * 3000.0 / 180.0;
    double PI = 3.1415926535897932384626;
    double a = 6378245.0;
    double ee = 0.00669342162296594323;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("Position Translate");
    connect(ui->pushButton  ,SIGNAL(clicked()),this,SLOT(pushbutton1()));
    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(pushbutton2()));
    connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(pushbutton3()));
}
MainWindow::~MainWindow()
{
    delete ui;
}
bool MainWindow::outof_China(double lon, double lat)
{
    return(lon<72.004 || lon>137.8374 || lat<0.8293 || lat >55.8271 || false);
}
POSITION MainWindow::bd09togcj02(double bd_lon, double bd_lat)
{
    double x = bd_lon - 0.0065;
    double y = bd_lat - 0.006;
    double z = sqrt(x*x + y*y) - 0.00002*sin(y*x_PI);
    double theta = atan2(y,x) - 0.000003*cos(x*x_PI);
    gcj_pos.longitude = z*cos(theta);
    gcj_pos.latitude = z*sin(theta);
    return gcj_pos;
}
POSITION MainWindow::gcj02tobd09(double gcj_lon, double gcj_lat)
{
     double z = sqrt(gcj_lon*gcj_lon + gcj_lat*gcj_lat) + 0.00002*sin(gcj_lat * x_PI);
     double theta = atan2(gcj_lat,gcj_lon) + 0.000003 * cos(gcj_lon * x_PI);
     bd_pos.longitude = z*cos(theta) + 0.0065;
     bd_pos.latitude = z*sin(theta) + 0.006;
     return bd_pos;
}
double MainWindow::translate_lon(double lon, double lat)
{
    double ret = 300.0 + lon +2.0*lat + 0.1*lon*lon +0.1*lon*lat + 0.1*sqrt(abs(lon));
    ret += (20.0 * sin(6.0*lon*PI) + 20.0*sin(2.0*lon*PI)) *2.0 / 3.0;
    ret += (20.0 * sin(lon*PI) + 40.0*sin(lon/3.0 *PI)) *2.0 /3.0;
    ret += (150 * sin(lon/12.0 *PI) + 300.0*sin(lon/30.0 * PI)) *2.0 /3.0;
    return ret;
}
double MainWindow::translate_lat(double lon, double lat)
{
    double ret = -100 + 2.0*lon + 3.0*lat + 0.2*lat*lat + 0.1*lon*lat + 0.2*sqrt((abs(lon)));
    ret += (20.0 *sin(6.0*lon*PI) + 20*sin(2.0*lon*PI)) *2.0 /3.0;
    ret += (20.0 *sin(lat*PI) + 40.0*sin(lat/3.0*PI)) *2.0 /3.0;
    ret += (160.0*sin(lat/12.0*PI) + 320.0*sin(lat/30.0 *PI)) *2.0 /3.0;
    return ret;
}
POSITION MainWindow::gcj02towgs84(double gcj_lon, double gcj_lat)
{
    if(outof_China(gcj_lon,gcj_lat))
    {
        wgs_pos.longitude = gcj_lon;
        wgs_pos.latitude = gcj_lat;
        return wgs_pos;
    }
    else
    {
        double dlat = translate_lat(gcj_lon - 105.0,gcj_lat -35.0);
        double dlon = translate_lon(gcj_lon - 105.0,gcj_lat -35.0);
        double radlat = gcj_lat/180.0 *PI;
        double magic = sin(radlat);
        magic = 1 - ee*magic*magic;
        double squrtmagic = sqrt(magic);
        dlon = (dlon *180.0)/(a/squrtmagic*cos(radlat)*PI);
        dlat = (dlat *180.0)/((a*(1-ee))/(magic * squrtmagic)*PI);
        wgs_pos.longitude = gcj_lon - dlon;
        wgs_pos.latitude = gcj_lat - dlat;
        return wgs_pos;
    }
}
POSITION MainWindow::wgs84togcj02(double wgs_lon, double wgs_lat)
{
    if(outof_China(wgs_lon,wgs_lat))
    {
        gcj_pos.longitude = wgs_lon;
        gcj_pos.latitude = wgs_lat;
        return gcj_pos;
    }
    else
    {
        double dlat = translate_lat(wgs_lon - 105.0,wgs_lat - 35.0);
        double dlon = translate_lon(wgs_lon - 105.0,wgs_lat - 35.0);
        double radlat = wgs_lat/180.0 * PI;
        double magic = sin(radlat);
        magic = 1 - ee*magic*magic;
        double squrtmagic = sqrt(magic);
        dlon = (dlon *180.0)/(a/squrtmagic*cos(radlat)*PI);
        dlat = (dlat *180.0)/((a*(1-ee))/(magic * squrtmagic)*PI);
        gcj_pos.longitude = wgs_lon + dlon;
        gcj_pos.latitude = wgs_lat +dlat;
        return gcj_pos;
    }
}
/*************************************************************************
 *
 * 讀取 BD09 坐標(biāo),轉(zhuǎn)換為 WGS84 和 GCJ02 坐標(biāo)
 *
 ***********************************************************************/
void MainWindow::pushbutton1()
{
    double bd09_lon = ui->lineEdit_bd09lon->text().toDouble();
    double bd09_lat = ui->lineEdit_bd09lat->text().toDouble();
    double gcj02_lon = bd09togcj02(bd09_lon,bd09_lat).longitude;
    double gcj02_lat = bd09togcj02(bd09_lon,bd09_lat).latitude;
    double wgs84_lon = gcj02towgs84(gcj02_lon,gcj02_lat).longitude;
    double wgs84_lat =gcj02towgs84(gcj02_lon,gcj02_lat).latitude;
    ui->lineEdit_gcj02lon->setText(QString::number(gcj02_lon,'d',9));
    ui->lineEdit_gcj02lat->setText(QString::number(gcj02_lat,'d',9));
    ui->lineEdit_wgs84lon->setText(QString::number(wgs84_lon,'d',9));
    ui->lineEdit_wgs84lat->setText(QString::number(wgs84_lat,'d',9));
}
/*************************************************************************
 *
 * 讀取 GCJ02 坐標(biāo),轉(zhuǎn)換為 WGS84 和 BD09 坐標(biāo)
 *
 ***********************************************************************/
void MainWindow::pushbutton2()
{
    double gcj02_lon = ui->lineEdit_gcj02lon->text().toDouble();
    double gcj02_lat = ui->lineEdit_gcj02lat->text().toDouble();
    double bd09_lon = gcj02tobd09(gcj02_lon,gcj02_lat).longitude;
    double bd09_lat = gcj02tobd09(gcj02_lon,gcj02_lat).latitude;
    double wgs84_lon = gcj02towgs84(gcj02_lon,gcj02_lat).longitude;
    double wgs84_lat = gcj02towgs84(gcj02_lon,gcj02_lat).latitude;
    ui->lineEdit_bd09lon->setText(QString::number(bd09_lon,'d',9));
    ui->lineEdit_bd09lat->setText(QString::number(bd09_lat,'d',9));
    ui->lineEdit_wgs84lon->setText(QString::number(wgs84_lon,'d',9));
    ui->lineEdit_wgs84lat->setText(QString::number(wgs84_lat,'d',9));
}
/*************************************************************************
 *
 * 讀取 WGS84 坐標(biāo),轉(zhuǎn)換為 GCJ02 和 BD09 坐標(biāo)
 *
 ***********************************************************************/
void MainWindow::pushbutton3()
{
    double wgs84_lon = ui->lineEdit_wgs84lon->text().toDouble();
    double wgs84_lat = ui->lineEdit_wgs84lat->text().toDouble();
    double gcj02_lon = wgs84togcj02(wgs84_lon,wgs84_lat).longitude;
    double gcj02_lat = wgs84togcj02(wgs84_lon,wgs84_lat).latitude;
    double bd09_lon = gcj02tobd09(gcj02_lon,gcj02_lat).longitude;
    double bd09_lat = gcj02tobd09(gcj02_lon,gcj02_lat).latitude;
    ui->lineEdit_bd09lon->setText(QString::number(bd09_lon,'d',9));
    ui->lineEdit_bd09lat->setText(QString::number(bd09_lat,'d',9));
    ui->lineEdit_gcj02lon->setText(QString::number(gcj02_lon,'d',9));
    ui->lineEdit_gcj02lat->setText(QString::number(gcj02_lat,'d',9));
}

算法來源于網(wǎng)上搜集,運(yùn)行結(jié)果如下:

C++怎么實(shí)現(xiàn)百度坐標(biāo)及GCJ02與WGS84之間的轉(zhuǎn)換

感謝各位的閱讀,以上就是“C++怎么實(shí)現(xiàn)百度坐標(biāo)及GCJ02與WGS84之間的轉(zhuǎn)換”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對C++怎么實(shí)現(xiàn)百度坐標(biāo)及GCJ02與WGS84之間的轉(zhuǎn)換這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI