一、准備
Google Earth提供了個人免費版、Plus版、Pro版,個人開發只安裝個人免費版就可以了,如果需要更 多的功能,那麼只有每年上交$400購買專業版了
到目前為止,GoogleEarth的二次開發接口還比較少,功能太弱,僅僅提供了1.0的類庫。
GoogleEarth COM API參考文檔可以在這裡找到:http://earth.google.com/comapi/index.html
C#調用COM的參考資料多如牛毛,大家可以到網上搜一下
二、例子
這裡提供一個利用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: /// <summary>
23: /// 標記GE是否已經啟動
24: /// </summary>
25: private bool isGeStarted = false;
26: /// <summary>
27: /// 定義GE應用程序類
28: /// </summary>
29: private ApplicationGEClass GeApp;
30:
31: public Form1()
32: {
33: InitializeComponent();
34: }
35:
36: private void button1_Click(object sender, EventArgs e)
37: {
38: StartGE();
39: }
40:
41: /// <summary>
42: /// 啟動GE
43: /// </summary>
44: private void StartGE()
45: {
46: if (isGeStarted)
47: {
48: return;
49: }
50:
51: try
52: {
53: GeApp = (ApplicationGEClass)Marshal.GetActiveObject ("GoogleEarth.Application");
54:
55: isGeStarted = true;
56: }
57: catch
58: {
59: GeApp = new ApplicationGEClass();
60:
61: isGeStarted = true;
62: }
63: }
64:
65: private void button2_Click(object sender, EventArgs e)
66: {
67: string ssFile = Path.Combine(Application.StartupPath, "ScreenShot.jpg");
68:
69: try
70: {
71: //quality的取值范圍在(0,100)之間,質量越高,quality越大
72: GeApp.SaveScreenShot(ssFile, 100);
73:
74: MessageBox.Show("成功保存截屏圖像:" + ssFile);
75: }
76: catch(Exception ex)
77: {
78: MessageBox.Show("保存截屏圖像時發生錯誤:" + ex.Message);
79: }
80: }
81:
82: private void button3_Click(object sender, EventArgs e)
83: {
84: string ssFile = Path.Combine(Application.StartupPath, "ScreenShot.jpg");
85:
86: if (!File.Exists(ssFile))
87: {
88: MessageBox.Show("未能找到保存的截屏圖像!");
89: return;
90: }
91:
92: Process.Start(ssFile);
93: }
94:
95: private void button4_Click(object sender, EventArgs e)
96: {
97: this.Close();
98: Application.Exit();
99: }
100:
101: }
102: }