C#调用GoogleEarth COM API

开发 后端
本文介绍了C#调用GoogleEarth COM API的准备工作及示例。

一、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保存一张截图,然后可以打开这个截图看看。呵呵)

 

  1. // 功能:GE实例     
  2.  // 描述:GE COM API 网址:http://earth.google.com/comapi/index.html  
  3.  // 作者:温伟鹏  
  4.  // 日期:2008-01-20  
  5.    
  6.  using System;  
  7.  using System.Collections.Generic;  
  8.  using System.ComponentModel;  
  9.  using System.Data;  
  10. using System.Drawing;  
  11. using System.Text;  
  12. using System.Windows.Forms;  
  13. using EARTHLib;  
  14. using System.Runtime.InteropServices;  
  15. using System.IO;  
  16. using System.Diagnostics;  
  17.  
  18. namespace GEDemo  
  19. {  
  20.   public partial class Form1 : Form  
  21.     {  
  22.         ///   
  23.         /// 标记GE是否已经启动  
  24.         /// 
  25.  
  26.         private bool isGeStarted = false;  
  27.         ///   
  28.         /// 定义GE应用程序类  
  29.         /// 
  30.  
  31.         private ApplicationGEClass GeApp;  
  32.  
  33.         public Form1()  
  34.         {  
  35.             InitializeComponent();  
  36.         }  
  37.  
  38.         private void button1_Click(object sender, EventArgs e)  
  39.         {  
  40.             StartGE();   
  41.         }  
  42.  
  43.         ///    
  44.         /// 启动GE  
  45.         /// 
  46.  
  47.         private void StartGE()  
  48.         {  
  49.             if (isGeStarted)  
  50.             {  
  51.                 return;  
  52.             }  
  53.  
  54.             try 
  55.             {  
  56.                 GeApp = (ApplicationGEClass)Marshal.GetActiveObject("GoogleEarth.Application");  
  57.  
  58.                 isGeStarted = true;  
  59.             }  
  60.             catch   
  61.             {  
  62.                GeApp = new ApplicationGEClass();  
  63.  
  64.                 isGeStarted = true;  
  65.             }  
  66.         }  
  67.  
  68.         private void button2_Click(object sender, EventArgs e)   
  69.         {  
  70.             string ssFile = Path.Combine(Application.StartupPath, "ScreenShot.jpg");  
  71.  
  72.             try 
  73.             {  
  74.                //quality的取值范围在(0,100)之间,质量越高,quality越大  
  75.                 GeApp.SaveScreenShot(ssFile, 100);  
  76.  
  77.                 MessageBox.Show("成功保存截屏图像:" + ssFile);  
  78.             }  
  79.             catch(Exception ex)  
  80.             {  
  81.                 MessageBox.Show("保存截屏图像时发生错误:" + ex.Message);  
  82.             }  
  83.         }  
  84.  
  85.         private void button3_Click(object sender, EventArgs e)  
  86.         {  
  87.            string ssFile = Path.Combine(Application.StartupPath, "ScreenShot.jpg");  
  88.    
  89.             if (!File.Exists(ssFile))  
  90.             {  
  91.                 MessageBox.Show("未能找到保存的截屏图像!");  
  92.                 return;  
  93.             }  
  94.  
  95.             Process.Start(ssFile);  
  96.         }  
  97.  
  98.         private void button4_Click(object sender, EventArgs e)  
  99.         {  
  100.             this.Close();  
  101.             Application.Exit();  
  102.         }  
  103.  
  104.     }  

【编辑推荐】

  1. C#程序中的数据显示:自定义标签和XML、XSL
  2. C#自定义事件是如何生成的
  3. C# 自定义控件dll文件的生成步骤
  4. C#自定义快捷键的实现
  5. C#自定义事件的步骤介绍
责任编辑:book05 来源: cnblogs
相关推荐

2009-08-05 15:37:01

调用Windows A隐藏GoogleEar

2009-08-21 17:45:40

C#调用COM对象

2009-08-03 11:32:49

C#调用COM对象

2009-09-02 16:43:55

C#调用Excel的C

2009-08-03 13:34:57

C#调用Windows

2009-07-31 16:12:10

Windows APIC#

2009-08-07 16:10:20

C#调用API

2009-08-03 13:13:52

C#调用Outlook

2009-09-24 15:10:54

C#调用COM组件

2009-08-21 17:42:36

C#调用API

2009-08-03 14:09:15

C#调用API

2009-08-17 13:18:01

C#调用Windows

2009-09-18 19:09:41

C# COM组件

2009-07-31 15:47:20

Win32 APIC#

2009-08-25 16:16:27

C#调用Windows

2009-08-19 14:35:12

C++和C#相互调用C

2009-08-11 14:16:00

Winform调用WEC#

2009-08-17 13:49:20

C#中调用Window

2009-08-17 13:26:20

C#调用Windows

2009-08-20 11:03:34

Visual C#使用
点赞
收藏

51CTO技术栈公众号