ITeye博客

mapxtreme添加标记和删除标记

seven7680 · 2009-03-30 16:23 · MapXtreme · Markdown

新增2个pointselectiontool, clientcommand设为MapCommand,clientinteraction设为ClickInteraction, command一个设为:AddPinPointCommand,一个设为ClearPinPointCommand,

page_load添加

复制内容到剪贴板 代码:

controlModel.Commands.Add(new AddPinPointCommand()); controlModel.Commands.Add(new ClearPinPointCommand()); MapInfo.Mapping.Map myMap = GetMapObj(); if (myMap != null) { if (myMap.Layers != null) { myMap.Layers.Remove(SampleConstants.TempLayerAlias); } } // Need to clean up "dirty" temp table left by other customer requests. MapInfo.Engine.Session.Current.Catalog.CloseTable(SampleConstants.TempTableAlias); // Need to clear the DefautlSelection. MapInfo.Engine.Session.Current.Selections.DefaultSelection.Clear(); // Creat a temp table and AddPintPointCommand will add features into it. MapInfo.Data.TableInfoMemTable ti = new MapInfo.Data.TableInfoMemTable(SampleConstants.TempTableAlias); // Make the table mappable ti.Columns.Add(MapInfo.Data.ColumnFactory.CreateFeatureGeometryColumn(myMap.GetDisplayCoordSys())); ti.Columns.Add(MapInfo.Data.ColumnFactory.CreateStyleColumn()); MapInfo.Data.Table table = MapInfo.Engine.Session.Current.Catalog.CreateTable(ti); // Create a new FeatureLayer based on the temp table, so we can see the temp table on the map. myMap.Layers.Insert(0, new FeatureLayer(table, "templayer", SampleConstants.TempLayerAlias)); 添加一个方法:

复制内容到剪贴板 代码:

private MapInfo.Mapping.Map GetMapObj() { // Get the map MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory; if (myMap == null) { myMap = MapInfo.Engine.Session.Current.MapFactory; } return myMap; } 在customizedcommands.cs中添加(取info时新增过这个类,把下面代码加进去)

复制内容到剪贴板 代码:

/// /// Summary description for PinPointCommand. ///

public class AddPinPointCommand : MapInfo.WebControls.MapBaseCommand
{
    /// <summary>
    /// Constructor for this command, sets the name of the command
    /// </summary>
    /// <remarks>None</remarks>
    public AddPinPointCommand()
    {
        Name = "AddPinPointCommand";
    }
    /// <summary>
    /// This method gets the map object out of the mapfactory with given mapalias and 
    /// Adds a point feature into a temp layer, exports it to memory stream and streams it back to client.
    /// </summary>
    /// <remarks>None</remarks>
    public override void Process()
    {
        // Extract points from the string
        System.Drawing.Point points = this.ExtractPoints(this.DataString);
        MapControlModel model = MapControlModel.GetModelFromSession();
        model.SetMapSize(MapAlias, MapWidth, MapHeight);
        MapInfo.Mapping.Map map = model.GetMapObj(MapAlias);
        if (map == null) return;
        // There will be only one point, convert it to spatial
        MapInfo.Geometry.DPoint point;
        map.DisplayTransform.FromDisplay(points, out point);
        IMapLayer lyr = map.Layers;
        if (lyr == null)
        {
            TableInfoMemTable ti = new TableInfoMemTable(SampleConstants.TempTableAlias);
            // Make the table mappable
            ti.Columns.Add(ColumnFactory.CreateFeatureGeometryColumn(map.GetDisplayCoordSys()));
            ti.Columns.Add(ColumnFactory.CreateStyleColumn());
           MapInfo .Data.Table   table = MapInfo.Engine.Session.Current.Catalog.CreateTable(ti);
            map.Layers.Insert(0, new FeatureLayer(table, "templayer", SampleConstants.TempLayerAlias));
        }
        lyr = map.Layers;
        if (lyr == null) return;
        FeatureLayer fLyr = lyr as FeatureLayer;
        MapInfo.Geometry.Point geoPoint = new MapInfo.Geometry.Point(map.GetDisplayCoordSys(), point);
        // Create a Point style which is a red pin point.
        SimpleVectorPointStyle vs = new SimpleVectorPointStyle();
        vs.Code = 67;
        vs.Color = Color.Red;
        vs.PointSize = Convert.ToInt16(24);
        vs.Attributes = StyleAttributes.PointAttributes.BaseAll;
        vs.SetApplyAll();
       
        // Create a Feature which contains a Point geometry and insert it into temp table.
        Feature pntFeature = new Feature(geoPoint, vs);
        MapInfo.Data.Key key = fLyr.Table.InsertFeature(pntFeature);
        // Send contents back to client.
        MemoryStream ms = model.GetMap(MapAlias, MapWidth, MapHeight, ExportFormat);
        StreamImageToClient(ms);
    }
}
/// <summary>
/// Summary description for PinPointCommand.
/// </summary>

public class ClearPinPointCommand : MapInfo.WebControls.MapBaseCommand
{
    /// <summary>
    /// Constructor for this command, sets the name of the command
    /// </summary>
    /// <remarks>None</remarks>
    public ClearPinPointCommand()
    {
        Name = "ClearPinPointCommand";
    }
    /// <summary>
    /// This method gets the map object out of the mapfactory with given mapalias 
    /// and This method delete the pin point features added by AddPinPointCommand in a given point 
    /// and then streams the image back to client.
    /// </summary>
    /// <remarks>None</remarks>
    public override void Process()
    {
        System.Drawing.Point points = ExtractPoints(DataString);
        MapControlModel model = MapControlModel.GetModelFromSession();
        model.SetMapSize(MapAlias, MapWidth, MapHeight);
        MapInfo.Mapping.Map map = model.GetMapObj(MapAlias);
        if (map == null) return;
        PointDeletion(map, points);
        MemoryStream ms = model.GetMap(MapAlias, MapWidth, MapHeight, ExportFormat);
        StreamImageToClient(ms);
    }
    /// <summary>
    /// Delete a feature in the temporary layer.
    /// </summary>
    /// <param name="mapAlias">MapAlias of the map</param>
    /// <param name="point">oint in pixels</param>
    private void PointDeletion(Map map, System.Drawing.Point point)
    {
        // Do the search and show selections
        SearchInfo si = MapInfo.Mapping.SearchInfoFactory.SearchNearest(map, point, 10);
        (si.SearchResultProcessor as ClosestSearchResultProcessor).Options = ClosestSearchOptions.StopAtFirstMatch;
        MapInfo.Data.Table table = MapInfo.Engine.Session.Current.Catalog;
        if (table != null)
        {
            IResultSetFeatureCollection ifc = Session.Current.Catalog.Search(table, si);
            foreach (Feature f in ifc)
            {
                table.DeleteFeature(f);
            }
            ifc.Close();
        }
    }
}

/// <summary>
/// Summary description for SampleConstants.
/// </summary>
public class SampleConstants
{
    public static string TempLayerAlias = "inPointLayer";
    public static string TempTableAlias = "inPointTable";
    private SampleConstants() { }
}

运行会提示找不到:SimpleVectorPointStyle所引用的类,这是引用的mapinfo.styles 再运行会提示找不到:Session,这是引用的mapinfo.engine

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。