一、C#调用GoogleEarth COM API准备
Google Earth提供了个人免费版、Plus版、Pro版,个人开发只安装个人免费版就可以了,如果需要更多的功能,那么只有每年上交$400购买专业版了
到目前为止,GoogleEarth的二次开发接口还比较少,功能太弱,仅仅提供了1.0的类库。
GoogleEarth COM API参考文档可以在这里找到:http://earth.google.com/comapi/index.html
C#调用COM的参考资料多如牛毛,大家可以到网上搜一下
二、C#调用GoogleEarth COM API例子
这里提供一个利用VS2008 + Google Earth 5.0开发一个“Hello world”程序
首先,确保已经正确安装GE,打开VS2008 ,新建一个Windows应用程序项目,在“项目”菜单中选择“添加引用…”,切换到“COM”选项卡,选择“Google Earth 1.0 Type Library”,其实就是Google Earth的主程序
在项目的引用中你可以看到已经添加了一个EARTHLib的引用,然后我们就可以调用其中的接口进行开发了。
下面就是小例子的代码(功能很简单,只有三个,打开GE,然后让GE保存一张截图,然后可以打开这个截图看看。呵呵)
- // 功能:GE实例
- // 描述:GE COM API 网址:http://earth.google.com/comapi/index.html
- // 作者:温伟鹏
- // 日期:2008-01-20
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using EARTHLib;
- using System.Runtime.InteropServices;
- using System.IO;
- using System.Diagnostics;
- namespace GEDemo
- {
- public partial class Form1 : Form
- {
- ///
- /// 标记GE是否已经启动
- ///
- private bool isGeStarted = false;
- ///
- /// 定义GE应用程序类
- ///
- private ApplicationGEClass GeApp;
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- StartGE();
- }
- ///
- /// 启动GE
- ///
- private void StartGE()
- {
- if (isGeStarted)
- {
- return;
- }
- try
- {
- GeApp = (ApplicationGEClass)Marshal.GetActiveObject("GoogleEarth.Application");
- isGeStarted = true;
- }
- catch
- {
- GeApp = new ApplicationGEClass();
- isGeStarted = true;
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- string ssFile = Path.Combine(Application.StartupPath, "ScreenShot.jpg");
- try
- {
- //quality的取值范围在(0,100)之间,质量越高,quality越大
- GeApp.SaveScreenShot(ssFile, 100);
- MessageBox.Show("成功保存截屏图像:" + ssFile);
- }
- catch(Exception ex)
- {
- MessageBox.Show("保存截屏图像时发生错误:" + ex.Message);
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- string ssFile = Path.Combine(Application.StartupPath, "ScreenShot.jpg");
- if (!File.Exists(ssFile))
- {
- MessageBox.Show("未能找到保存的截屏图像!");
- return;
- }
- Process.Start(ssFile);
- }
- private void button4_Click(object sender, EventArgs e)
- {
- this.Close();
- Application.Exit();
- }
- }
- }
【编辑推荐】