MapObjects使用Buffer函数生成缓冲区,并返回一个pologon。需要注意的是,Buffer函数的第二个参数,即一个地图范围,通常设置为Map.Extent,但该extent必须能够包含所生成的缓冲区,否则缓冲区无法生成,返回null。
为避免缓冲区生成失败,可以认为设定Buffer的extent参数。以点状shape的缓冲区生成为例,可以获取缓冲区的包络矩形的范围,然后使得extent参数的范围能够完全容纳这个矩形即可。
代码如下
If listCity.Text <> "" Then
Dim distance As Double '缓冲距离
Dim leftside As Double
Dim rightside As Double
Dim topside As Double
Dim bottomside As Double
Dim rect As MapObjects2.Rectangle
Dim p As MapObjects2.Point
Dim city As New MapObjects2.Recordset
distance = 1050000
Set city = cityLayer.SearchExpression("mc = '" + listCity.Text + "'") 'mc = '" + listCity.Text + "'
If Not city Is Nothing Then
city.MoveFirst
If Not city.EOF Then
Set p = city.Fields("shape").Value
MsgBox (p.X)
leftside = p.X - distance
rightside = p.X + distance
topside = p.Y + distance
bottomside = p.Y - distance
Set rect = New Rectangle
If leftside < map.Extent.Left Then
rect.Left = leftside - 1 '不能重叠,所以-1
Else
rect.Left = map.Extent.Left
End If
If rightside > map.Extent.Right Then
rect.Right = rightside + 1
Else: rect.Right = map.Extent.Right
End If
If topside > map.Extent.Top Then
rect.Top = topside + 1
Else: rect.Top = map.Extent.Top
End If
If bottomside < map.Extent.Bottom Then
rect.Bottom = bottomside - 1
Else: rect.Bottom = map.Extent.Bottom
End If
' map.Extent = rect
'map.Refresh
Set bufferCircle = p.Buffer(distance, rect)
MsgBox (bufferCircle.Area)
End If
MsgBox (city.Count)
MsgBox (listCity.Text)
End If
map.TrackingLayer.Refresh True
Else
MsgBox "请首先选中一个地级市!"
End If
End Sub
注意,不能让缓冲区的包络矩形的一侧正好与extent重叠,否则无法生成,既extent要“真包含”这一矩形。在程序中,可适当加减一个数值即可,本例中为1.