将Google Earth加载入Qt地图界面

将Google Earth加载入Qt地图界面

本文采用Qt5.9.3,创建简单界面,能够借助Com组件打开Google Earth.exe应用程序,可进行更深层开发,做出地图显示界面。虽然Google Earth在新版本中已经将Api接口关闭,但是老版还是可以用的,这个使那个老版安装包,我上传说我资源重复,大家自行查找吧,官网应该也有的。世上本没有路,走到人多了就成了路,大家共同使用谷歌地球,说不定哪天接口又会开启了呢。
这里写图片描述

  • Com基础知识及Google Earth相关书籍
  • 程序效果图
  • 程序讲解
  • 代码出处

Com基础知识及面向对象编程思想

一些基本东西我就不多说了,提供几个关键词,可自行查阅: Com对象和接口,面向对象编程思想,《智慧地球:Google Earth/Maps/Kml核心开发技术揭秘》,《Google API开发详解:Google Maps与Google Earth双剑合璧(第2版)》 书中有更加详细的Google Earth API讲解。

程序效果图

这里写图片描述
点击按钮打开谷歌地球
这里写图片描述

程序讲解

老套路,不多说了直接上代码:
google.h文件

#ifndef GOOGLE_H
#define GOOGLE_H

#include <QMainWindow>
#include <guiddef.h>
//#include "windows.h"
#include "googleearth.tlh"

#import "C:\Program Files (x86)\Google\Google Earth\client\googleearth.exe" no_namespace

static const CLSID CLSID_ApplicationGE ={0x8097D7E9,0xDB9E,0x4AEF, {0x9B,0x28,0x61,0xD8,0x2A,0x1D,0xF7,0x84}};
static const CLSID IID_IApplicationGE ={0x2830837B,0xD4E8,0x48C6, {0xB6,0xEE,0x04,0x63,0x33,0x72,0xAB,0xE4}};


namespace Ui {
class Google;
}

class Google : public QMainWindow
{
    Q_OBJECT

public:
    explicit Google(QWidget *parent = 0);
    ~Google();

    static void KillProcess(const QString& strProcName);
public slots:

    void StartGE(void);

private:
    IApplicationGE* m_GEApp;

    HWND m_GEParentHandle;//GE视图父窗口句柄
    HWND m_GEHandle;    //GE视图区窗口句柄
    HWND m_GeMainHandle;//GE主窗体句柄

    Ui::Google *ui;
};

#endif // GOOGLE_H

google.cpp文件

#include "google.h"
#include "ui_google.h"
#include "windows.h"
#include "windef.h"
#include "tchar.h"


Google::Google(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Google)
{
    ui->setupUi(this);

    //试图清除其他事先运行的GE
    this->KillProcess("Google Earth");
    this->KillProcess("GoogleEarth");
    this->KillProcess("GoogleCrashHandler");

    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(StartGE()));
}

Google::~Google()
{
    delete ui;
}


void Google::KillProcess(const QString &strProcName)
{
    DWORD dwProcID;
    HANDLE hProcess;
    TCHAR wProcName[260];
    int Len = strProcName.toWCharArray(wProcName);
    wProcName[Len] = _T('\0');
    HWND h = FindWindow(0,wProcName);
    if(h)
    {
        GetWindowThreadProcessId(h,&dwProcID);
        hProcess = OpenProcess(PROCESS_TERMINATE,JOB_OBJECT_ASSIGN_PROCESS,dwProcID);
        TerminateProcess(hProcess,0);
    }
}


void Google::StartGE()
{
    HRESULT hr;
    hr = CoCreateInstance(CLSID_ApplicationGE,NULL,CLSCTX_LOCAL_SERVER,IID_IApplicationGE,(void**) &m_GEApp);
    if(SUCCEEDED(hr))
    {
        m_GeMainHandle = (HWND)m_GEApp->GetMainHwnd();//隐藏启动的GE
        ::SetWindowPos(m_GeMainHandle, HWND_BOTTOM, 0 , 0, 0, 0, SWP_NOSIZE|SWP_HIDEWINDOW);
        m_GEHandle =(HWND) m_GEApp->GetRenderHwnd();//截获GE的视图区
        RECT rect;
        ::GetWindowRect((HWND)this->winId(), &rect);//取得当前视图窗口客户区大小
        m_GEParentHandle = ::GetParent(m_GEHandle);//保存GE视图区旧的父窗体句柄
        ::SetParent(m_GEHandle, (HWND)this->winId());//截获GE视图

        ::SetWindowPos(m_GeMainHandle,HWND_BOTTOM,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top+50,SWP_FRAMECHANGED);//显示GE
    }
}

具体说明代码里面都有,这里唯一需要注意以下,编译器采用VC编译器,因为MinGW的话,需要用qt自带的ActiceX什么类(具体忘记了),比较费事,采用VC的话,可以直接#import “exe”,然后自动生成tlh与tli文件。运行时候需要先将#include “googleearth.tlh”注释掉,因为还没有生成tlh文件,编译之后,将其回复,则可以调用IApplicationGE* m_GEApp类了。剩下的看看书,再做一些二次开发就可以了,顺带提一下结合kml文件也是非常不错的选择。

代码出处

点这里

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
加载本地sqlite瓦片影像地图png可以通过以下步骤实现: 1. 在Qt中使用QSqlDatabase连接sqlite数据库。 ```c++ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("your_database_name.sqlite"); if (!db.open()) { qDebug() << "Failed to open database."; return; } ``` 2. 在osgEarth中使用Sqlite驱动器打开数据库,并加载图层。 ```c++ osgEarth::Registry::instance()->setDriverModel("sqlite"); osgEarth::ImageLayerOptions layerOptions("your_layer_name", "your_database_name.sqlite"); layerOptions.setDriver("sqlite"); // 设置图层的范围、瓦片大小和最大级别等参数 layerOptions.setProfile(osgEarth::ProfileOptions::create("global-geodetic")); layerOptions.setMinLevel(0u); layerOptions.setMaxLevel(18u); layerOptions.setTileSize(256u); // 设置图层的URL格式 layerOptions.addImageLayerOptions().driver()->setUrl("sqlite:///"+your_database_name.sqlite+"/your_table_name?column=your_column_name"); // 创建图层并添加到地球模型中 osgEarth::ImageLayer* layer = new osgEarth::ImageLayer(layerOptions); osgEarth::Map* map = new osgEarth::Map(); map->addLayer(layer); osgEarth::Viewer viewer; viewer.setSceneData(map->getTerrainEngine()); viewer.run(); ``` 3. 如果你的sqlite数据库中存储的是png格式的瓦片,那么只需要在URL中添加`&format=png`即可。 ```c++ layerOptions.addImageLayerOptions().driver()->setUrl("sqlite:///"+your_database_name.sqlite+"/your_table_name?column=your_column_name&format=png"); ``` 这样就可以加载本地sqlite瓦片影像地图png了。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值