MapWindow 相关

来源:互联网 发布:淘宝上药店 编辑:程序博客网 时间:2024/04/19 19:32


http://www.mapwindow.org/documentation/mapwingis4.8/class_shapefile.html

MapWindow随写(MapWinGIS)

mapwindow  是开源免费的GIS控件

一.准备工作

1. 注册com组件
http://www.mapwindow.org/ 获取最新控件.
Regsvr32.exe 路径/MapWinGIS.ocx   
2..新建项目,添加引用
 
 
3.添加控件
 

 

 


二.正式编程
1.载入地图
Map Control 控件拖入window form,取名:mainMap。

//图层1返回句柄
Int  inthandler1;

//实例化形状文件
MapWinGIS.Shapefile shapefile1 = new MapWinGIS.Shapefile();

//打开文件
shapefile1.Open(@"D:\GISSampleData\base.shp", null );

//载入地图文件,true显示
intHandler1 = mainMap.AddLayer(shapefile1, true);
 载入成功
2.缩放功能

mainMap.CursorMode = MapWinGIS.tkCursorMode.xxx;
tkCursorModem  枚举:
•    cmZoomIn       放大
•    cmZoomOut      缩小
•    cmPan          手装选择
•    cmSelection    选中

•    cmNone         无状态

 

 

3 设置点 图层的 颜色 大小 类型
//设置点的颜色,大小,类型 方法(map对象的方法)
mainMap.set_ShapeLayerPointSize(pointHandle, 5);
mainMap.set_ShapeLayerPointColor(pointHandle,(uint)ColorTranslator.ToOle(Color.FromA#ff0000));

mainMap.set_ShapeLayerPointType(pointHandle,tkPointType.ptCircle);

显示出红色的点

 

4 设置图层填充颜色  线颜色 宽度

//设置背景填充色,线颜色,线宽 方法(map对象的方法)
            mainMap.set_ShapeLayerFillColor(cityHandle, (uint)ColorTranslator.ToOle(Color.FromA#37172c));
            mainMap.set_ShapeLayerLineColor(cityHandle, (uint)ColorTranslator.ToOle(Color.FromA#9b643c));
            mainMap.set_ShapeLayerLineWidth(cityHandle, 5);
效果图 


 

5 给点加上自定义图片

给点加上图片:
//实例化图片类型
MapWinGIS.Image  pointImage = new MapWinGIS.Image();
            pointImage.Open(@"C:\Documents and Settings\lyp\My Documents\Visual Studio 2008\Projects\MapDemo\MapDemo\ico\hand1.JPG", ImageType.JPEG_FILE, true, null);

//设置使用自定义类型
mainMap.set_ShapeLayerPointType(pointHandle, tkPointType. ptUserDefined);

//设置用户自定义的点图片类型
mainMap.set_UDPointType(pointHandle,pointImage);

参考效果 
6  给不同类型的点,加上自定义图片

//定义两张不同的图片
MapWinGIS.Image  cityImage= new MapWinGIS.Image();
MapWinGIS.Image  normalImage= new MapWinGIS.Image();
cityImage.Open(“路径”);
normalImage.Open(“图片路径”);

//获得index
int intCityIndex;
int intNormalIndex
intCityIndex=mainMap.set_UDPointImageListAdd(cityHandle, cityImage);
intNormalIndex= mainMap.set_UDPointImageListAdd(NormalHandle, normalImage);


//在数据库中字段的排号,这儿假设为2
int filedNum = 3

int isCity;

//sf前缀表示shapefile类型
  for (int ShapeNum = 0; ShapeNum < sfCitys.NumShapes;ShapeNum++ ) 
{
    isCity= sfCitys.CellValue(filedNum, ShapeNum);
    if(isCity==1)
        {
            mainMap.set_ShapePointImageListID(cityHandle, ShapeNum, intCityIndex);
}
    Else
        {
            mainMap.set_ShapePointImageListID(NormalHandle, ShapeNum, intNormalIndex);
}
};   

效果图:
 

7 显示标签
string labelText = string.Empty;
for (int i = 0; i < pointShape.NumShapes; i++)
{
            labelText = pointShape.get_CellValue(filedNum, i).ToString();
             x = pointShape.get_Shape(i).get_Point(0).x;
             y = pointShape.get_Shape(i).get_Point(0).y;
   mainMap.AddLabel(pointHandle, labelText, (uint)ColorTranslator.ToOle(Color.FromA#000000), x, y, tkHJustification.hjCenter);
            }

//设置碰撞检测
   mainMap.set_UseLabelCollision(pointHandle, true);

参考效果图:

 



8 LegendControl控件,显示图层
 
1.    引用MapWinInterfaces.dll文件
2.    控件栏添加该控件
3.    拖入windowform

LegendControl.Legend legend;
legend.Map = (MapWinGIS.Map)mainMap.GetOcx();

//设置图层名称
legend.Map.set_LayerName(cityHandle, System.IO.Path.GetFileNameWithoutExtension(city.Filename));
legend.Map.set_LayerName(pointHandle, System.IO.Path.GetFileNameWithoutExtension(pointShape.Filename));
对于map,相当于代理,里面的方法,属性都相差无几。

效果图      

 

 

9 map上画线
1.    地图上要激发鼠标事件,必须先设置endMouseXXX 事件
mainMap.SendMouseDown = true;
mainMap.SendMouseMove = true;
mainMap.SendMouseUp = true;

2.画线获取起点
private void mainMap_MouseDownEvent(object sender, AxMapWinGIS._DMapEvents_MouseDownEvent e)
        {
            mainMap.PixelToProj(e.x, e.y, ref startX, ref startY);
            draw = true;
        }

PixelToProj ()方法转换  为地图相对应的 点。
(startX,startY)  起点, startX 就是地图的经度,startY就是纬度。于是在移动的时候做了个显示经纬度的。
 
4.    获取终点

int  draw_hndl;
  private void mainMap_MouseUpEvent(object sender, AxMapWinGIS._DMapEvents_MouseUpEvent e)
        {
 mainMap.PixelToProj(e.x, e.y, ref endX, ref endY);

//画图之前,必须有这段代码
 draw_hndl = mainMap.NewDrawing(MapWinGIS.tkDrawReferenceList.dlSpatiallyReferencedList);

//画线
 mainMap.DrawLine(startX, startY, endX, endY, 1, (uint)ColorTranslator.ToOle(Color.FromA#000000));
        }

效果图: 


10 获取地图数据

 

11.经纬度定位

          //获取地图区域
            MapWinGIS.Extents myExtents = (Extents)mainMap.Extents;

           //设置经纬度范围
  myExtents.SetBounds(114.38, 30.62, 0, 114.39, 30.632956891957239, 0);
            
           //定位该范围  
mainMap.Extents = myExtents;
          
 //设置缩放级别,因为定位的不同,缩放级别肯定不同,比如城市,小区,的缩放级别不同
            mainMap.CurrentScale = 0.025;


地名定位:获取数据库中该地名的shape的 ID,然后获取该shape的x y(经纬度),然后在用该方法定位。



三.参考资料

http://www.mapwindow.org

 

1.UsingMapWinGIS.pdf  初学入门

2. MapWinGIS Reference Manual A function guide for the free MapWindow GIS ActiveX map component.pdf

下载地址

Mapwindows控件api详解


 

 

如若转载,请保留作者wallini