C#调用Google Earth COM API开发(一)

来源:互联网 发布:安卓手机网络共享 编辑:程序博客网 时间:2024/06/05 17:40

一、准备

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using EARTHLib;
using System.Diagnostics;
using System.Runtime.InteropServices;

//////////////////////////////////////////////////////////////////////////

namespace GMapDemo
{
    /// <summary>
    /// ​G​E​实​例
    /// </summary>
    public partial class Form1 : Form
    {

        private bool isGeStarted = false;

        private ApplicationGEClass GeApp;



        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StartGE();
        }

        private void StartGE()
        {
            if(isGeStarted)
            {
                return;
            }
            try
            {
                //GeApp = (ApplicationGEClass)Marshal.GetActiveObject("GoogleEarth.Application");
                GeApp = new ApplicationGEClass();
                isGeStarted = true;
            }
            catch (System.Exception ex)
            {
                //GeApp = (ApplicationGEClass)Marshal.GetActiveObject("GoogleEarth.Application");
                //isGeStarted = true;
               
            }
            finally
            {

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            String ss = Path.Combine(Application.StartupPath, "ScreenShot.jpg");
            try
            {
                GeApp.SaveScreenShot(ss, 100);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("保存截屏图像时发生错误:" + ex.Message);
            }
            finally
            {
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            String ss = Path.Combine(Application.StartupPath, "ScreenShot.jpg");
            if(!File.Exists(ss))
            {
                MessageBox.Show("未能找到截屏图像!");
                return;

            }
            Process.Start(ss);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }
       
       
    }
}

原创粉丝点击