本文采用Qt5.9.3,创建简单界面,能够借助Com组件打开Google Earth.exe应用程序,可进行更深层开发,做出地图显示界面。虽然Google Earth在新版本中已经将Api接口关闭,但是老版还是可以用的,这个使那个老版安装包,我上传说我资源重复,大家自行查找吧,官网应该也有的。世上本没有路,走到人多了就成了路,大家共同使用谷歌地球,说不定哪天接口又会开启了呢。
一些基本东西我就不多说了,提供几个关键词,可自行查阅: Com对象和接口,面向对象编程思想,《智慧地球:Google Earth/Maps/Kml核心开发技术揭秘》,《Google API开发详解:Google Maps与Google Earth双剑合璧(第2版)》 书中有更加详细的Google Earth API讲解。
老套路,不多说了直接上代码:
google.h文件
#ifndef GOOGLE_H
#define GOOGLE_H
#include
#include
//#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文件也是非常不错的选择。
点这里