MapWinGIS使用

这几天要在程序中添加一个显示地图的功能。Boss说是在给客户演示的时候比较....额。看上去比较正式.

看了一天GDAL各种看不懂。很多博客的教程都是说怎么用GDALShp数据,图层属性,改其中的字段之类的,找不到有介绍怎么把地图显示到界面上的文章。于是自己摸索着用了一个开源的 MapWinGIS.ocx 控件来显示地图 简要的记录一下过程.

注册控件mapwingis.ocx 

   regsvr32  ..\.路径\mapwingis.ocx

VS2010里添加控件 

添加方法 : 工具栏上点击右键->选择项->COM组件->勾选Map Control

添加MapControl控件调整属性

2 stdafx.h添加如下代码 (MapWinGIS.ocx控件在当前目录的MapGis SDK目录下)

#import "..\MapWinGIS_SDK\MapWinGIS.ocx" rename_namespace("mapWindow") rename("GetObject""GISGetObject")

2  InitInstance 函数中初始化OLE库

  1. // 初始化 OLE 库  
  2. if (!AfxOleInit())  
  3. {  
  4.     //AfxMessageBox(IDP_OLE_INIT_FAILED);  
  5.     return FALSE;  
  6. }  


3  添加一个ActiveX控件中的MFC类  CGisMap

 

包含 CGisMap类所在的头文件 实例化一个对象

CGisMap m_Map;

5 添加一个打开地图按钮测试效果

  1. //打开图像  
  2. void CDlgMap::OnBnClickedOk()  
  3. {  
  4.     char  szFilter[] = "GeoTiff (*.shp)|*.shp|All Files (*.*)|*.*||";  
  5.     CString filePath("");  
  6.     CFileDialog fileOpenDlg(TRUE, "shp", NULL,OFN_HIDEREADONLY,szFilter);  
  7.     if (fileOpenDlg.DoModal() == IDOK)   
  8.     {  
  9.         if (filePath =  fileOpenDlg.GetPathName())  
  10.         {  
  11.             if (filePath.IsEmpty())  
  12.             {  
  13.                 return;  
  14.             }  
  15.         }  
  16.     }  
  17.           
  18.   
  19.     // 创建shp接口打开shp文件加入地图  
  20.     mapWindow::IShapefilePtr pShapefilebound;  
  21.     pShapefilebound.CreateInstance(__uuidof(mapWindow::Shapefile));  
  22.     DWORD  dd = GetLastError();  
  23.     pShapefilebound->Open(_bstr_t(filePath),false);  
  24.   
  25.     m_Map.AddLayer(pShapefilebound,true);  
  26. }  


注意:

在exe中初始化ole 只需要

  1. if (!AfxOleInit())  
  2. {  
  3. //AfxMessageBox(IDP_OLE_INIT_FAILED);  
  4. return FALSE;  
  5. }  


而在dll中初始化ole则麻烦一些  如果初始化不成功。在调用 pShapefilebound.CreateInstance(__uuidof(mapWindow::Shapefile));

时候会失败!.


  1. <pre name="code" class="cpp">BOOL CMapVialGdalApp::InitInstance()  
  2. {  
  3.     HRESULT hr = OleInitialize(NULL);   
  4.     if(hr ==S_FALSE)   
  5.     {   
  6.         OleUninitialize();   
  7.     }   
  8.     //Call if  using  OLE  Controls   
  9.     AfxEnableControlContainer();   
  10.     //   Register    all    OLE    server    (factories)    as    running.    This    enables    the   
  11.     //    OLE    libraries    to    create    objects    from    other    applications.   
  12.     COleObjectFactory::RegisterAll();   
  13.     return CWinApp::InitInstance();  
  14.     return TRUE;  
  15. }</pre><br>  
  16. <br>  
  17. <pre></pre>  
  18. <p></p>  
  19. <pre></pre>  
  20. <p></p>  
  21. <pre></pre>  
  22. <p></p>  
  23. <p>对话框的类在OnInitDialog中还要调用一次</p>  
  24. <p>AfxEnableControlContainer();   如果不添加就会在C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\mfc\occcont.cpp 的 InitControlContainer函数中如下位置报错。..我很菜,不明白为什么.....</p>  
  25. <p></p>  
  26. <p>TRY</p>  
  27. <p>  {</p>  
  28. <p><span style="white-space:pre"></span><span style="white-space:pre"></span><span style="white-space:pre"></span>m_pCtrlCont = afxOccManager->CreateContainer(<span style="color:rgb(0,0,255)">this</span>);</p>  
  29. <p>  }</p>  
  30. <p>  END_TRY<br>  
  31. <br>  
  32. <br>  
  33. <br>  
  34. <br>  
  35. <span style="font-family:Arial; font-size:14px; line-height:26px; text-align:left; white-space:pre">/************************************************/</span><br style="font-family:Arial; font-size:14px; line-height:26px; text-align:left; white-space:pre">  
  36. <span style="font-family:Arial; font-size:14px; line-height:26px; text-align:left; white-space:pre"></span><span style="font-family:Arial; font-size:14px; line-height:26px; text-align:left; white-space:pre">`╰Witchつ  ---2012年5月30日</span><br style="font-family:Arial; font-size:14px; line-height:26px; text-align:left; white-space:pre">  
  37. <span style="font-family:Arial; font-size:14px; line-height:26px; text-align:left; white-space:pre"></span><span style="font-family:Arial; font-size:14px; line-height:26px; text-align:left; white-space:pre">  http://blog.csdn.net/Witch_Soya</span><br style="font-family:Arial; font-size:14px; line-height:26px; text-align:left; white-space:pre">  
  38. <span style="font-family:Arial; font-size:14px; line-height:26px; text-align:left; white-space:pre"></span><span style="font-family:Arial; font-size:14px; line-height:26px; text-align:left; white-space:pre">/************************************************/</span><br>  
  39. </p>  
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值