zl程序教程

您现在的位置是:首页 >  其他

当前栏目

MapXtreme 2005 学习心得 一些基础函数代码(四)

基础代码 函数 一些 学习心得 2005
2023-09-14 08:59:38 时间
ExpandedBlockStart.gif
ExpandedBlockStart.gif///  summary
    /// 添加线[代码和创建点的相差无几]
    /// by 路过秋天
    ///  param name="layerName" 图层名 /param
    ///  param name="startPoint" 线段起点坐标 /param
    ///  param name="endPoint" 线段终点坐标 /param
    ///  param name="shortCode" 线的shortCode(线的型状也有多种,比如单箭头,双箭头等) /param
    ///  param name="color" 线的颜色 /param
    ///  /summary
    public static void AddLine(string layerName, DPoint startPoint, DPoint endPoint, int shortCode, Color color)
ExpandedBlockStart.gif    {
        MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[0];

        //获取图层和表
        FeatureLayer workLayer = (MapInfo.Mapping.FeatureLayer)myMap.Layers[layerName];
        MapInfo.Data.Table table = workLayer.Table;

        //创建线
        FeatureGeometry line = MultiCurve.CreateLine(workLayer.CoordSys, startPoint, endPoint);
        //以下两行是图形的样式
        MapInfo.Styles.SimpleLineStyle slsLine = new MapInfo.Styles.SimpleLineStyle(new LineWidth(3, LineWidthUnit.Pixel), shortCode, color);
        MapInfo.Styles.CompositeStyle lineStyle = new MapInfo.Styles.CompositeStyle(slsLine);
        //接下来创建一行数据
        MapInfo.Data.Feature ptLine = new MapInfo.Data.Feature(table.TableInfo.Columns);
        ptLine.Geometry = line;
        ptLine.Style = lineStyle;
        ptLine["index"] = new Random().Next(999); ;
        ptLine["value"] = "this is a line";
        //将线图元加入图层
        table.InsertFeature(ptLine);
    }

 

三:显示标注文本

1:显示标注文本函数代码:ShowValue

ExpandedBlockStart.gif
ExpandedBlockStart.gif ///  summary
    /// 显示标注
    /// by 路过秋天
    ///  param name="tableName" 标注的表名 /param
    ///  param name="columnName" 标注的列名 /param
    ///  /summary
    public static void ShowValue(string tableName, string columnName)
ExpandedBlockStart.gif    {
        MapInfo.Mapping.Map myMap = MapInfo.Engine.Session.Current.MapFactory[0];

        //新建标注图层并绑定数据(整个过程有点像DataGrid控件指定数据源控件SqlDataSource,而数据源控件又绑定了DataTable)
        LabelLayer labelLayer = new LabelLayer();
        myMap.Layers.Add(labelLayer);

        //指定要标注的数据表
        MapInfo.Data.Table table = MapInfo.Engine.Session.Current.Catalog.GetTable(tableName);

        LabelSource source = new LabelSource(table);//绑定Table
        labelLayer.Sources.Append(source);//加载指定数据

        //指定哪个字段作为显示标注(在非必备的自定义列里挑一个,比如我们就挑"value"列)
        source.DefaultLabelProperties.Caption = columnName;

        //标注样式等属性,注意这段注释的代码,是指在一定的缩放比例范围内才显示文本,要是不注释掉,可能折腾半天也看不到为啥显示不出来文本
        //source.DefaultLabelProperties.Visibility.Enabled = true;
        //source.DefaultLabelProperties.Visibility.VisibleRangeEnabled = true;
        //source.DefaultLabelProperties.Visibility.VisibleRange = new VisibleRange(0.01, 10, MapInfo.Geometry.DistanceUnit.Mile);

        source.DefaultLabelProperties.Visibility.AllowDuplicates = true;
        source.DefaultLabelProperties.Visibility.AllowOverlap = true;
        source.DefaultLabelProperties.Visibility.AllowOutOfView = true;
        source.Maximum = 50;
        source.DefaultLabelProperties.Layout.UseRelativeOrientation = true;
        source.DefaultLabelProperties.Layout.RelativeOrientation = MapInfo.Text.RelativeOrientation.FollowPath;
        source.DefaultLabelProperties.Layout.Angle = 33.0;
        source.DefaultLabelProperties.Layout.Offset = 7;
        source.DefaultLabelProperties.Layout.Alignment = MapInfo.Text.Alignment.CenterCenter;
        MapInfo.Styles.Font font = new MapInfo.Styles.Font("黑体", 12);
        font.ForeColor = System.Drawing.Color.Red;
        source.DefaultLabelProperties.Style.Font = font;
    }

 

先上这四个最基本的函数,如果把这几个函数放一个类中,别忘了加名称空间

using MapInfo.Geometry;
using MapInfo.Mapping;
using MapInfo.Styles;
using MapInfo.Data;
using MapInfo.Text;
using System.Drawing;