将Google Earth加载彩票平台搭建入Qt地图界面

将Google Earth加载彩票平台搭建论坛:haozbbs.com Q1446595067 入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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

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
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

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

猜你喜欢

转载自blog.51cto.com/13857115/2137671