Google Earth 嵌入Qt应用程序

Google Earth 嵌入Qt应用程序

专业术语

COM组件:COM是Component Object Mode的缩写,是微软定义的接口标准,遵循该标准的小程序是COM组件,它可以是独立的可执行二进制程序,也可以是动态链接库等。
Google Earth:它是Google公司推出的一款桌面地图产品,二次开发可以基于基于Google EarthComAPI 和Google EarthWeb Plugin两种方式,本文介绍第一种方式。

前提准备

1,安装Google Earth客户端
Google Earth已经在新版本的客户端中将API接口进行了关闭,但是老版本还是可以继续使用的,本地采用的客户端版本是GoogleEarth_7.1.2.2041.exe(有需要的可以向我获取),不用修改任何参数,一直点击下一步即可完成软件安装;
打开客户端测试是否能够连接Google服务,正常显示地球画面(需要等待一段时间),如果一直显示黑屏,需要修改C:\Windows\System32\drivers\etc下的hosts文件增加Google服务器地址(来自BIGEMAP地图下载器),如下;

#############################################################
#
# (c) 2015 BM
#
# Date: 2020/03/19 09:58
#
# 将生成的 hosts 文件复制到 C:/Windows/System32/drivers/etc 目录中即可
#
# (可备份原 hosts 文件)
#
#############################################################
203.208.50.71 www.google.com
203.208.50.71 geoauth.google.com
203.208.50.71 support.google.com
203.208.50.71 accounts.google.com
203.208.50.71 apis.google.com
203.208.50.71 auth.keyhole.com
203.208.50.71 kh.google.com
203.208.50.71 kh.l.google.com
203.208.50.71 khm.google.com
203.208.50.71 khm.l.google.com
203.208.50.71 khm0.google.com
203.208.50.71 khm1.google.com
203.208.50.71 khm2.google.com
203.208.50.71 khm3.google.com
203.208.50.71 khmdb.google.com
203.208.50.71 khms.google.com
203.208.50.71 khms.l.google.com
203.208.50.71 khms0.google.com
203.208.50.71 khms1.google.com
203.208.50.71 khms2.google.com
203.208.50.71 khms3.google.com
203.208.50.71 mt.google.com
203.208.50.71 mt.l.google.com
203.208.50.71 mt0.google.com
203.208.50.71 mt1.google.com
203.208.50.71 mt2.google.com
203.208.50.71 mt3.google.com
203.208.50.71 mts.google.com
203.208.50.71 mts.l.google.com
203.208.50.71 mts0.google.com
203.208.50.71 mts1.google.com
203.208.50.71 mts2.google.com
203.208.50.71 mts3.google.com
203.208.50.71 mw1.google.com
203.208.50.71 mw2.google.com
203.208.50.71 lh0.ggpht.com
203.208.50.71 lh1.ggpht.com
203.208.50.71 lh2.ggpht.com
203.208.50.71 lh3.ggpht.com
203.208.50.71 lh4.ggpht.com
203.208.50.71 lh5.ggpht.com
203.208.50.71 lh6.ggpht.com
203.208.50.71 www.gstatic.com
203.208.50.71 ssl.gstatic.com
203.208.50.71 csi.gstatic.com
203.208.50.71 maps.gstatic.com
203.208.50.71 fonts.gstatic.com
203.208.50.71 mt0.google.cn
203.208.50.71 mt1.google.cn
203.208.50.71 mt2.google.cn
203.208.50.71 mt3.google.cn
#END

2,导入COM组件
COM组件导入有两种方法:通过导入类库的方法和Import的方式。
导入类库:在工程项目中,添加类,类型选择(TypeLib中的MFC类),选择来源自文件,输入文件路径(C:\Program Files (x86)\Google\Google Earth\client\googleearth.exe),可以将COM组件中的接口导入到指定类中,如下图(貌似只有MFC项目才能使用该方法);
导入COM组件
Import方式导入:直接在代码中通过#import “.exe”的方式将COM组件接口导入,编译器(需要支持COM组件编程)编译的时候会生成一个.tlh文件和一个.tli,前一个是头文件件定义,后一个是函数实现,我们只需要在自己个工程中引用头文件即可,本文介绍的也是这种方式。(注意:第一次编译的时候先将第一个include包含注释掉,等Import指令生成tlh文件后再放开)

#include "googleearth.tlh"
 
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}};
 
//#import "C:\Program Files (x86)\Google\Google Earth\client\googleearth.exe" no_namespace

实现方案

1,创建COM对象,用指定的类标识符创建一个未初始化的对象;
2,获取Google Earth操作句柄,将其放到Qt应用程序的窗口中显示;
3,测试获取某个点的经纬度及海拔信息,相关代码及效果如下;

HRESULT hr;
hr = CoCreateInstance(CLSID_ApplicationGE,NULL,CLSCTX_LOCAL_SERVER,IID_IApplicationGE,(void**) &m_GEApp);
if(SUCCEEDED(hr))
{
    bool onlie = m_GEApp->IsInitialized();
    onlie = m_GEApp->IsOnline();
    m_GeMainHandle = (HWND)m_GEApp->GetMainHwnd();
    ::SetWindowPos(m_GeMainHandle, HWND_BOTTOM, 0 , 0, 0, 0, SWP_NOSIZE|SWP_HIDEWINDOW);
    m_GEHandle =(HWND) m_GEApp->GetRenderHwnd();
    RECT rect;
    ::GetWindowRect((HWND)this->winId(), &rect);
    m_GEParentHandle = ::GetParent(m_GEHandle);
    ::SetParent(m_GEHandle, (HWND)this->winId());
 
    ::SetWindowPos(m_GeMainHandle,HWND_BOTTOM,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top+50,SWP_FRAMECHANGED);
}
// 设置相机参数
m_GEApp->SetCameraParams(dlatitude, dlongtitude, 0, RelativeToGroundAltitudeGE, 0, 0, 0, 5);
IPointOnTerrainGEPtr pointPtr = m_GEApp->GetPointOnTerrainFromScreenCoords(0, 0);
if (nullptr != pointPtr)
{
    height = pointPtr->GetAltitude();
}

实现效果
在这里插入图片描述

遇到问题

COM组件获取IApplicationGE对象时,提示组件未注册
Google并没有提供给用户独立的COM组件,而是在用户安装了Google Earth程序后,自动将自带的动态链接库注册到用户的操作系统上,此时就可以调用API了;
如果测试发现未注册成果,需要重新安装Google Earth客户端,或者在命令行进入到Google Earth安装目录下输入”googleearth.exe /RegServer”,注意命令行要用管理员身份运行,否则会报权限不足的错误。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值