MapWinGIS使用.

来源:互联网 发布:shader编程教程 编辑:程序博客网 时间:2024/06/05 03:39

这几天要在程序中添加一个显示地图的功能。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库

// 初始化 OLE 库if (!AfxOleInit()){//AfxMessageBox(IDP_OLE_INIT_FAILED);return FALSE;}


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

 

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

CGisMap m_Map;

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

//打开图像void CDlgMap::OnBnClickedOk(){char  szFilter[] = "GeoTiff (*.shp)|*.shp|All Files (*.*)|*.*||";CString filePath("");CFileDialog fileOpenDlg(TRUE, "shp", NULL,OFN_HIDEREADONLY,szFilter);if (fileOpenDlg.DoModal() == IDOK) {if (filePath =  fileOpenDlg.GetPathName()){if (filePath.IsEmpty()){return;}}}// 创建shp接口打开shp文件加入地图mapWindow::IShapefilePtr pShapefilebound;pShapefilebound.CreateInstance(__uuidof(mapWindow::Shapefile));DWORD  dd = GetLastError();pShapefilebound->Open(_bstr_t(filePath),false);m_Map.AddLayer(pShapefilebound,true);}


注意:

在exe中初始化ole 只需要

if (!AfxOleInit()){//AfxMessageBox(IDP_OLE_INIT_FAILED);return FALSE;}


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

时候会失败!.


BOOL CMapVialGdalApp::InitInstance(){HRESULT hr = OleInitialize(NULL); if(hr ==S_FALSE) { OleUninitialize(); } //Call if  using  OLE  Controls AfxEnableControlContainer(); //   Register    all    OLE    server    (factories)    as    running.    This    enables    the //    OLE    libraries    to    create    objects    from    other    applications. COleObjectFactory::RegisterAll(); return CWinApp::InitInstance();return TRUE;}


对话框的类在OnInitDialog中还要调用一次

AfxEnableControlContainer();   如果不添加就会在C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\mfc\occcont.cpp 的 InitControlContainer函数中如下位置报错。..我很菜,不明白为什么.....

TRY

  {

m_pCtrlCont = afxOccManager->CreateContainer(this);

  }

  END_TRY




/************************************************/
`╰Witchつ  ---2012年5月30日
  http://blog.csdn.net/Witch_Soya
/************************************************/

原创粉丝点击