MarkPoints.cs

This example demonstrates how to how to add markers for the points of interest. The marker represented by image loaded from the file will be added on the mouse down event. The markers will be stored in the temporary in-memory shapefile. Here is a screenshot with the results of the code.

MarkPoints.png
´╗┐using System;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
using System.IO;

public partial class MapExamples
{
    // the handle of the layer with markers
    private int m_layerHandle = -1;
    
    // <summary>
    // Loads the layers and registers event handlers
    // </summary>
    public void MarkPoints(AxMap axMap1, string dataPath)
    {
        if (this.AddLayersFromPath(axMap1, 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;
            MapEvents.MouseDownEvent += new AxMapWinGIS._DMapEvents_MouseDownEventHandler(axMap1_MouseDownEvent);   // change MapEvents to axMap1
        }
    }

    // <summary>
    // Opens a marker from the file
    // </summary>
    private Image OpenMarker()
    {
        string path = @"..\..\icons\marker.png";
        if (!File.Exists(path))
        {
            MessageBox.Show("Can't find the file: " + path);
        }
        else
        {
            Image img = new 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;
    }

    // <summary>
    // Handles mouse down event and adds the marker
    // </summary>
    public void axMap1_MouseDownEvent(object sender, _DMapEvents_MouseDownEvent e)
    {
        if (e.button == 1)          // left button
        {
            Shapefile sf = axMap1.get_Shapefile(m_layerHandle);

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

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

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