mapxtreme如何添加标记和删除标记呢?
新增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[SampleConstants.TempLayerAlias] != 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[MapControl1.MapAlias];
if (myMap == null)
{
myMap = MapInfo.Engine.Session.Current.MapFactory[0];
}
return myMap;
}
在customizedcommands.cs中添加(取info时新增过这个类,把下面代码加进去)
/// <summary>
/// Summary description for PinPointCommand.
/// </summary>
[Serializable]
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[0], out point);
IMapLayer lyr = map.Layers[SampleConstants.TempLayerAlias];
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[SampleConstants.TempLayerAlias];
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();
//BitmapPointStyle vs = new BitmapPointStyle();
//vs.Name = @"C:\Documents and Settings\董霆\桌面\GIS\sample\images\star.gif";
//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>
[Serializable]
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[0]);
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">Point 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[SampleConstants.TempTableAlias];
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 = "PinPointLayer";
public static string TempTableAlias = "PinPointTable";
private SampleConstants() { }
}
运行会提示找不到:SimpleVectorPointStyle所引用的类,这是引用的mapinfo.styles
再运行会提示找不到:Session,这是引用的mapinfo.engine。
OK!此文章为在下在互联网上摘抄整理。