---
title: "mapxtreme添加标记和删除标记"
author: "seven7680"
date: "2009-03-30T16:23:13Z"
tags: ["F#", "UP"]
slug: "mapxtreme"
draft: false
lastmod: "2009-03-30T16:23:13Z"
id: 357754
url: "https://seven7680.iteye.com/blog/357754"
---

新增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 description for PinPointCommand. /// \[Serializable] public class AddPinPointCommand : MapInfo.WebControls.MapBaseCommand { /// /// Constructor for this command, sets the name of the command /// /// None public AddPinPointCommand() { Name = "AddPinPointCommand"; } /// /// 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. /// /// None 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(); // 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 description for PinPointCommand. /// \[Serializable] public class ClearPinPointCommand : MapInfo.WebControls.MapBaseCommand { /// /// Constructor for this command, sets the name of the command /// /// None public ClearPinPointCommand() { Name = "ClearPinPointCommand"; } /// /// 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. /// /// None 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); } /// /// Delete a feature in the temporary layer. /// /// MapAlias of the map /// oint in pixels 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 description for SampleConstants. /// public class SampleConstants { public static string TempLayerAlias = "inPointLayer"; public static string TempTableAlias = "inPointTable"; private SampleConstants() { } } 运行会提示找不到：SimpleVectorPointStyle所引用的类，这是引用的mapinfo.styles 再运行会提示找不到：Session，这是引用的mapinfo.engine