C# 调用 Google Earth Com API开发(四)

本篇继续介绍Google Earth COM API开发的基础知识,相对第三篇的改进如下:

1)增加鼠标滚轮支持,可以实现放大、缩小。此功能利用上一篇提供的HookAPI.dll实现

2)读取PlaceMarks(Google Earth界面中的位置)并显示、隐藏

3)读取所有图层,显示并隐藏

下面,继续放代码:

1、鼠标滚轮事件,实现放大、缩小

   1: ...
   2: // 放大
   3: private const long ZoomIn = 0x00780000;
   4: // 缩小
   5: private const long ZoomOut = 0xFF880000;
   6: ...
   7: mouseHook.MouseWheel += new MouseEventHandler(mouseHook_MouseWheel);
   8: ...
   9: /// 
  10: /// 鼠标钩子。鼠标滚动事件
  11: /// 
  12: /// 
  13: /// 
  14: void mouseHook_MouseWheel(object sender, MouseEventArgs e)
  15: {
  16:     IntPtr hWnd = NativeMethods.WindowFromPoint(e.Location);
  17:     if (hWnd == this.GeRenderHWnd)
  18:     {
  19:         Point point = this.Control.PointToClient(e.Location);
  20:         // 如果鼠标位置在控件内部,则说明鼠标在GoogleEarth视图范围内进行了滚动
  21:         if (this.Control.ClientRectangle.Contains(point))
  22:         {
  23:             NativeMethods.PostMessage(GeRenderHWnd, (int)WM_MOUSE.WM_MOUSEWHEEL, e.Delta == 120 ? ZoomIn : ZoomOut, 0);
  24:         }
  25:     }
  26: }

2、读取PlaceMarks

Google Earth COM API中提供了两个读取PlaceMarks的函数。一个是GetTemporaryPlaces(),用来读取临时位置;另一个是GetMyPlaces(),用来读取自定义位置,即GoogleEarth中显示的“我的位置”。呵呵

   1: ...
   2: /// 
   3: /// 显示位置“PlaceMarks”。位置分为两种,一种时TemporaryPlaces,另一种为MyPlaces
   4: /// 
   5: protected void ShowPlaces()
   6: {
   7:     Thread.Sleep(500);
   8:     // 获取MyPlaces
   9:     FeatureGE myPlace = GeApp.GetMyPlaces();
  10:     // 获取临时位置
  11:     FeatureGE temporaryPlace = GeApp.GetTemporaryPlaces();
  12:     // 
  13:     List places = new List();
  14:     places.Add(myPlace);
  15:     places.Add(temporaryPlace);
  16:  
  17:     // 获取工具面板
  18:     GEToolPad toolPad = GetToolPad();
  19:     // 显示所有位置
  20:     toolPad.ShowPlaces(places);
  21: }
  22: ...
  23: /// 
  24: /// 显示所有PlaceMarks(位置)
  25: /// 
  26: /// 
  27: public void ShowPlaces(List places)
  28: {
  29:     if (this.InvokeRequired)
  30:     {
  31:         Action method = delegate {
  32:             InternalShowPlaces(places);
  33:         };
  34:  
  35:         try
  36:         {
  37:             this.Invoke(method);
  38:         }
  39:         catch { }
  40:     }
  41:     else
  42:     {
  43:         InternalShowPlaces(places);
  44:     }
  45: }
  46: /// 
  47: /// 显示所有PlaceMarks(位置)。被ShowPlaces函数调用
  48: /// 
  49: /// 
  50: protected void InternalShowPlaces(List places)
  51: {
  52:     this.tvPlaces.Nodes.Clear();
  53:  
  54:     if (places == null || places.Count <= 0)
  55:         return;
  56:  
  57:     foreach (FeatureGE place in places)
  58:     {
  59:         TreeNode node = new TreeNode(place.Name);
  60:         node.Checked = place.Visibility > 0;
  61:         node.Tag = place;
  62:  
  63:         ShowChildrenPlaces(place, node);
  64:  
  65:         node.Expand();
  66:  
  67:         this.tvPlaces.Nodes.Add(node);
  68:     }
  69: }
  70: /// 
  71: /// 显示指定PlaceMark的子PlaceMark
  72: /// 
  73: /// 父PlaceMark
  74: /// 父节点
  75: protected void ShowChildrenPlaces(FeatureGE place, TreeNode node)
  76: {
  77:     FeatureCollectionGE places = place.GetChildren();
  78:  
  79:     foreach (FeatureGE child in places)
  80:     {
  81:         TreeNode tn = new TreeNode(child.Name);
  82:         tn.Checked = child.Visibility > 0;
  83:         tn.Tag = child;
  84:  
  85:         ShowChildrenPlaces(child, tn);
  86:  
  87:         node.Nodes.Add(tn);
  88:     }
  89: }

3、读取图层

   1: ...
   2: /// 
   3: /// 显示图层
   4: /// 
   5: protected void ShowLayers()
   6: {
   7:     Thread.Sleep(500);
   8:     // 获取所有图层信息
   9:     FeatureCollectionGE layers = GeApp.GetLayersDatabases();
  10:     // 获取工具面板
  11:     GEToolPad toolPad = GetToolPad();
  12:     // 显示所有图层
  13:     toolPad.ShowLayers(layers);
  14: }
  15: ...
  16: /// 
  17: /// 将给定的图层集合显示在“图层”选项卡的树形控件中
  18: /// 
  19: /// 图层集合
  20: public void ShowLayers(FeatureCollectionGE layers)
  21: {
  22:     if (this.InvokeRequired)
  23:     {
  24:         Action method = delegate {
  25:             InternalShowLayers(layers);
  26:         };
  27:         try
  28:         {
  29:             this.Invoke(method);
  30:         }
  31:         catch { }
  32:     }
  33:     else
  34:     {
  35:         InternalShowLayers(layers);
  36:     }
  37: }
  38: /// 
  39: /// 将给定的图层集合显示在“图层”选项卡的树形控件中.被公共函数ShowLayers调用
  40: /// 
  41: /// 
  42: protected void InternalShowLayers(FeatureCollectionGE layers)
  43: {
  44:     this.tvLayers.Nodes.Clear();
  45:  
  46:     if (layers == null || layers.Count <= 0)
  47:     {
  48:         return;
  49:     }
  50:  
  51:     foreach (FeatureGE layer in layers)
  52:     {
  53:         TreeNode node = new TreeNode(layer.Name);
  54:         node.Checked = layer.Visibility > 0;
  55:         node.Tag = layer;
  56:  
  57:         ShowChidrenLayers(layer, node);
  58:  
  59:         node.Expand();
  60:  
  61:         this.tvLayers.Nodes.Add(node);
  62:     }
  63: }
  64: /// 
  65: /// 显示指定图层的子图层
  66: /// 
  67: /// 当前图层
  68: /// 当前节点
  69: protected void ShowChidrenLayers(FeatureGE layer, TreeNode node)
  70: {
  71:     FeatureCollectionGE layers = layer.GetChildren();
  72:  
  73:     foreach (FeatureGE child in layers)
  74:     {
  75:         TreeNode tn = new TreeNode(child.Name);
  76:         tn.Checked = child.Visibility > 0;
  77:         tn.Tag = child;
  78:  
  79:         ShowChidrenLayers(child, tn);
  80:  
  81:         node.Nodes.Add(tn);
  82:     }
  83: }

呵呵,代码很简单。按照此代码修改第三篇的源程序就可以实现本篇的功能了。

转载于:https://www.cnblogs.com/wpwen/archive/2009/05/25/1489144.html

你可能感兴趣的:(C# 调用 Google Earth Com API开发(四))