使用MapX开发实现若干小功能

一、  前言
我用 MapInfo公司的 MapX开发过几个项目。在项目的开发过程中,也接触了一些使用相同技术的公司和开发人员,感觉到他们的开发过程常常被一些问题所困扰,却得不到很好的解决。在这里,我把我在开发过程中积累的一些经验写出来,希望能起到抛砖引玉的作用。
二、开发经验
1 、如何在MapX 下读取属性值,这里介绍三种方法:
1)由 Layer对象的 KeyField属性来设立要读取属性值的字段名。接着,由 Feature对象的 keyValue读取此行的属性值。
2)将图层加入到 Datasets,由 Dataset对象的 Valuex,y)属性,通过设置行号,列号来获得属性值。
3)将图层加入到 Datasets之后由 RowValues(ftr)获取整行的值。
Dim ds As MapXLib.Dataset, lyr As MapXLib.layer
Dim ftrs As Features
Dim ftr As Feature
Dim rv As RowValue
Dim rvs As RowValues
Dim DsName As String '数据集名
Dim DsRows As Long, DsCols As Long
Dim i As Long, j As Long
Set ds = Formmain.Map1.Datasets.Item(DsName)
Set lyr = ds.layer
Set ftrs = lyr.AllFeatures
DsCols = ds.Fields.Count
DsCols = DsCols + 1
DsRows = ftrs.Count
Grid1.Rows = DsRows + 1
Grid1.Cols = DsCols
Grid1.Row = 0
For i = 0 To DsCols - 1
Grid1.Col = i
Grid1.Text = ds.Fields.Item(i + 1).Name
Next i
Grid1.Col = DsCols - 1
Grid1.Text = "Fkey"
lyr.BeginAccess miAccessRead
i = 1
For Each ftr In ftrs
Set rvs = ds.RowValues(ftr)
j = 0
For Each rv In rvs
If Not IsNull(rv.Value) Then Grid1.TextArray(i * DsCols + j) = Trim(rv.Value)
j = j + 1
Next
Grid1.TextArray(i * DsCols + j) = ftr.FeatureKey
i = i + 1
Next
lyr.EndAccess miAccessEnd
Set ftr = Nothing
Set ftrs = Nothing
Set ds = Nothing
Set rv = Nothing
Set rvs = Nothing
Set lyr = Nothing
注意: BeginAccess,以及 EndAccess可以明显的提高属性读取的速度。
2 、自定义范围专题图
MapX的专题图用户可以进行完全的定制,下面是自定义范围专题图的例子。
Dim ds As New MapXLib.Dataset
Dim thm As New MapXLib.Theme
Set ds = Formmain.Map1.Datasets(ToolBars.Combo2.Text)
Set thm = ds.Themes.add(0, "aa", "aa", False)
thm.Legend.Compact = False
thm.AutoRecompute = False
'thm.ComputeTheme = False
thm.DataMax = 700
thm.DataMin = 100
thm.ThemeProperties.AllowEmptyRanges = True
thm.ThemeProperties.NumRanges = 7
thm.ThemeProperties.DistMethod = miCustomRanges
thm.ThemeProperties.RangeCategories(1).Max = 150
thm.ThemeProperties.RangeCategories(1).Min = 50
thm.ThemeProperties.RangeCategories(2).Max = 250
thm.ThemeProperties.RangeCategories(2).Min = 150
thm.ThemeProperties.RangeCategories(3).Max = 350
thm.ThemeProperties.RangeCategories(3).Min = 250
thm.ThemeProperties.RangeCategories(4).Max = 450
thm.ThemeProperties.RangeCategories(4).Min = 350
thm.ThemeProperties.RangeCategories(5).Max = 550
thm.ThemeProperties.RangeCategories(5).Min = 450
thm.ThemeProperties.RangeCategories(6).Max = 650
thm.ThemeProperties.RangeCategories(6).Min = 550
thm.ThemeProperties.RangeCategories(7).Max = 750
thm.ThemeProperties.RangeCategories(7).Min = 650
'thm.ComputeTheme = True
thm.AutoRecompute = True
thm.Visible = True
3 、在Mapx 中查找对象有两种方式
1)使用 Find对象的 Search方法。在 MapX3.5中只能作到完全匹配查找,在 MapX4.0SearchEx方法则可以找到多个匹配的记录,其结果由  FindResult.Matches获取。详细请参看有关 Find.SearchEx 方法的文档以及示例。
2)使用 Layer 对象的 OBJECT.Search (strWhere)方法。其参数为 SQL查询的 WHERE子句。例如:
Set ftrs = lyr.Search("Character_Name = ""北京市 """) 
Set ftrs = lyrUSA.Search("TOTPOP > 1000000")
注意:
1)字符串外加两个双引号;
2)首先将图层加入数据集 Datasets 才能使用查询。
模糊查询最方便的方法是使用第二种方法,例如:
Set ftrs = lyr.Search("Character_Name like ""%""")
4 、在MapX 中如何紧缩表
Mapx4.51下可以使用 LayerInfo 的创建带结构的临时表和新表的功能来完成紧缩:
Set lyr = Formmain.Map1.Layers(ToolBars.combo1.Text)
Set ds = Formmain.Map1.Datasets.add(6, lyr)
'获取被紧缩表的路径及表名
filespec = Formmain.Map1.Layers.Item(ToolBars.combo1.Text).filespec
layername = lyr.Name '将表临时存放于内存
 
LayerInfo.Type = 6 'miLayerInfoTypeTemp
LayerInfo.AddParameter "TableStorageType", "MemTable" '临时文件保存在磁盘上还是内存。
LayerInfo.AddParameter "Name", "lyrpack"
LayerInfo.AddParameter "Fields", ds.Fields
LayerInfo.AddParameter "Features", lyr.AllFeatures
Formmain.Map1.Layers.add LayerInfo, LayerPos
Set LayerInfo = Nothing
'从地图窗口删除原表
Formmain.Map1.Datasets.Remove (ds.Name)
Formmain.Map1.Layers.Remove (lyr.Name)
Formmain.Map1.Refresh
Set lyr = Nothing
Set ds = Nothing
Set lyr = Formmain.Map1.Layers("lyrpack")
Set ds = Formmain.Map1.Datasets.add(6, lyr)
'从磁盘删除原表
Kill filespec
5 、在MapX 中如何使用自定义栅格符号
使用自定义符号首先需要设定 style.SymbolType miSymbolTypeBitmap,然后指定 SymbolBitmapName 为栅格图像名即可。
下面的代码演示了如何在 delphi中使用自定义的栅格符号
首先调用自定义工具画点
Procedure TForm1.new1Click(Sender: TObject);
Begin
map1.ControlInterface.CurrentTool :=111;
End;
tooluses事件中如下:
procedure TForm1.Map1ToolUsed(Sender: TObject; ToolNum: Smallint; X1, Y1,X2, Y2, Distance: Double; Shift, Ctrl: WordBool;
var EnableDefault: WordBool);
var
ssymbol :cmapxstyle;
p: CMapXPoint;
f: cmapxfeature;
begin
ssymbol:=costyle.create;
ssymbol.SymbolType :=1;
ssymbol.SymbolBitmapSize:=25;
{请注意将 test.bmp文件考到 mapx “共有文件路径” +\CUSTSYMB”路径下 ,例如 C:\Program Files\Common Files\MapInfo Shared\MapX Common  MapX 共有文件的缺省安装路径 }
ssymbol.SymbolBitmapName:='test.BMP';
p := CoPoint.Create;
f :=cofeature.Create ;
p.Set_(x1,y1);
if toolnum=111 then begin
f:=map1.ControlInterface.FeatureFactory.CreateSymbol(p,ssymbol);
map1.ControlInterface.Layers.Item(1).AddFeature(f,EmptyParam);
end;
end;
6 、在MapX 中如何使用自定义鼠标
MapX4.0,及以上版本中允许用户自定义鼠标。程序如下:
Map1.MousePointer = miCustomCursor
Map1.MouseIcon = "c:\windows\cursors\globe.ani"
MapX中还对鼠标滚动轮提供支持,属性如下:
Map.MouseWheelSupport=miMousewheelNoAutoScroll
三、结语

在写这篇东东之前,似乎脑子里充满了要写下的经验,但写的时候感觉很多都是可意会不可言传的东西,所以写的不多,希望以后有机会能慢慢整理。如果你采用Mapinfo公司系列产品开发地理信息型系统的话,我们可以进行深入的探讨。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值