将 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
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 文件也是非常不错的选择.
代码出处
来源: http://www.bubuko.com/infodetail-2675372.html