MapObjets2共有3种查询方法,样例在Query工程中。
2.4.1
逻辑表达式查询
在MapObjects的图层对象上可以用逻辑表达式进行查询,查询结果是Recordset实例。例1在图层china上查查询1990年gdp小于200的省份,将查询结果保存在记录集recs中,在Map1中用显示查到的省区图。
Dim recs As MapObjects2.Recordset ‘查询结果记录集
Private Sub Command1_Click() ‘用逻辑表达式查询
Dim query As String
query = “gdp1990 <
Set recs = Map1.Layers(“china”).SearchExpression(query) ‘查询,返回一个记录集
Map1.Refresh
End Sub
绘制查到的图形对象:
Private Sub Map1_AfterTrackingLayerDraw(ByVal hDC As stdole.OLE_HANDLE) ‘绘制查询结果图
If recs Is Nothing Then Exit Sub
Dim sym As New Symbol ‘绘图符号
Dim shp As Object
sym.Color = moRed
sym.SymbolType = moFillSymbol
sym.Style = moDiagonalCrossFill
Do While Not recs.EOF
Set shp = recs(“Shape”).Value
Map1.DrawShape shp, sym ‘ 绘制图查到的省区
recs.MoveNext
End Sub
MapObjects2不允许在Command1_Click()过程中使用绘图语句:
Map1.DrawShape shp, sym
因此,在Map1_AfterTrackingLayerDraw过程中绘制查询结果。在recs记录集中,含有记录的全部属性内容,属性值的读取方法在后续章节中介绍。
2.4.2
距离查询
根据图层的几何对象距离一个给定点的距离查询,结果是一个记录集。
用鼠标左键操作获取给定点的屏幕坐标:
Dim curX As Single, curY As Single ‘给定点的屏幕坐标
Private Sub Map1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
curX = X
curY = Y
End Sub
创建点对象,设定查询距离,查询china图层中的几何对象,获得查询结果recs:
Private Sub Command2_Click() ‘按距离查询
Dim pt As MapObjects2.Point
Dim dist As Double
Set pt = Map1.ToMapPoint(curX, curY) ‘根据屏幕坐标建立point实例
dist = 1000
Set recs = Map1.Layers(“china”).SearchByDistance(pt, dist, “”) ‘按距离查询
Map1.Refresh ‘触发Map1_AfterTrackingLayerDraw事件过程,绘制查询结果
End Sub
查询结果绘图显示使用前面的Map1_AfterTrackingLayerDraw过程。
由上面的程序可知,使用光盘中的样例时,应先用鼠标左键在地图窗口中点击要查的省区,然后单击按钮按距离查询。
按距离查询方法SearchByDistance的第三个参数可以是一个SQL表达式,用于筛选查询结果如:
Set recs = Map1.Layers(“china”).SearchByDistance(pt, dist, “gpd1985 <
在china图层中查找与pt的距离小于dist而且gpd1985 < 250的记录集合。
2.4.3
Shape查询
Shape查询方法
Map1.Layers(“china”).SearchShape( Shape As Object, SearchMethod as SearchMathodContants, Express ion As String )
是距离查询概念的扩充。Shape是几何元素,即point、line或polygon。SearchMethod是一个常数,表示Shape与被查询对象之间的空间关系,取值0-14,表示15种空间关系(在帮助中索引SearchMethod Constants);Expression是SQL表达式,过滤查询结果。
下面这段程序在地图窗口中跟踪一条线,查出线经过的省份。
Private Sub Command3_Click() ‘Shape查询Line与被查询对象相交
Dim line As MapObjects2.line
Map1.MousePointer = moCross
Set line = Map1.TrackLine ‘在地图窗口中绘制一条线,返回一个线对象实例
Set recs = Map1.Layers(“china”).SearchShape(line, moLineCross, “”) ‘查询,返回记录集
Map1.Refresh ‘重新显示地图窗口
Map1.MousePointer = moDefault
End Sub
语句
Set recs = Map1.Layers(“china”).SearchShape(line, moLineCross, “gdp1990 <
查找线line经过的省份且省的gdp1990 < 300的记录集。