MapWinGis学习(一) 新建图层 在指定点上打上图标

1.系统自带图标

void MapInit()
{

Shapefile towerShapfile = new Shapefile();


//创建新的图层

if (!towerShapfile.CreateNewWithShapeID("", ShpfileType.SHP_POINT)) //此处的第一个参数不为空就会出错,不知道为什么

{

Debug.Assert(true, "\r\nline 31 , CreateNewWithShapeID失败!\r\n");

return;

}


// 指定显示随机点的范围

double xMin = 0.0;

double yMin = 0.0;

double xMax = 1000.0;

double yMax = 1000.0;


// the location of points will be random

Random rnd = new Random(DateTime.Now.Millisecond);


//随机生成点

for (int i = 0; i < 10; i++)

{

MapWinGIS.Point pnt = new MapWinGIS.Point();

pnt.x = xMin + (xMax - xMin) * rnd.NextDouble();

pnt.y = yMin + (yMax - yMin) * rnd.NextDouble();


Shape shp = new Shape();

shp.Create(ShpfileType.SHP_POINT); //设置Shape的类型


int index = 0;

shp.InsertPoint(pnt, ref index); //将Point添加到Shape中

//shp.Create(ShpfileType.SHP_POINT);

towerShapfile.EditInsertShape(shp, ref i); //将Shape添加到Shapefile中

}


//设置towerShapfile图层上显示图标的类型为方块

towerShapfile.DefaultDrawingOptions.SetDefaultPointSymbol(tkDefaultPointSymbol.dpsSquare);

//添加图层

myAxMap.AddLayer(towerShapfile, true);


}


效果如下图:


2.自定义图片

 public void MarkPoints(AxMap axMap1, string dataPath)
        {
            Shapefile sf = new Shapefile();
            if (!sf.CreateNewWithShapeID("", ShpfileType.SHP_POINT))
            {
                MessageBox.Show("Failed to create shapefile: " + sf.get_ErrorMsg(sf.LastErrorCode));
                return;
            }


            m_layerHandle = axMap1.AddLayer(sf, true);


            ShapeDrawingOptions options = sf.DefaultDrawingOptions;
            options.PointType = tkPointSymbolType.ptSymbolPicture;
            options.Picture = this.OpenMarker();
            sf.CollisionMode = tkCollisionMode.AllowCollisions;


            axMap1.SendMouseDown = true;
            axMap1.CursorMode = tkCursorMode.cmNone;
            //MouseDownEvent += new AxMapWinGIS._DMapEvents_MouseDownEventHandler(myAxMap_MouseDownEvent);   // change MapEvents to axMap1












            Shapefile sf1 = axMap1.get_Shapefile(m_layerHandle);


            Shape shp = new Shape();
            shp.Create(ShpfileType.SHP_POINT);


            MapWinGIS.Point pnt = new MapWinGIS.Point();
            double x = 0.0;
            double y = 0.0;
            axMap1.PixelToProj(100, 100, ref x, ref y);
            pnt.x = x;
            pnt.y = y;
            int index = shp.numPoints;
            shp.InsertPoint(pnt, ref index);


            index = sf.NumShapes;
            if (!sf1.EditInsertShape(shp, ref index))
            {
                MessageBox.Show("Failed to insert shape: " + sf1.get_ErrorMsg(sf.LastErrorCode));
                return;
            }
            axMap1.Redraw();
        }


        // <summary>
        // Opens a marker from the file
        // </summary>
        private MapWinGIS.Image OpenMarker()
        {
            string path = "D:\\users\\wzp\\documents\\visual studio 2008\\Project\\MapWindow_CSharp4.8.8\\MapWindow_CSharp4.8.8\\ICON\\TOWER2-32.BMP";
            if (!File.Exists(path))
            {
                MessageBox.Show("Can't find the file: " + path);
            }
            else
            {
                MapWinGIS.Image img = new MapWinGIS.Image();
                if (!img.Open(path, ImageType.USE_FILE_EXTENSION, true, null))
                {
                    MessageBox.Show(img.get_ErrorMsg(img.LastErrorCode));
                    img.Close();
                }
                else
                    return img;
            }
            return null;
        }

你可能感兴趣的:(C#,MapWinGis)