绘制线条

本节内容可以参考CaseStudy目录下的文件"地图.Table"的示例三。

本节任务是绘制多条线条,用于连接多个城市:

示例:

1、将Map控件的"地图来源"属性设置为"Custom"

2、将Map控件的GetMapTile事件代码设置为:

Dim style As Integer = 8 '地图类型,可选值为610,其中6为卫星图
Dim
size As Integer = 1
Dim
scale As Integer = 1
Dim
server As Integer = Rand.Next(1, 5) '随机使用14号服务器
Dim
scl As Integer = 2 '设置为2可以隐藏地图的文字标注,且分辨率更好
Dim
Language As String = "zh_cn" '中文地图,如果需要英文可设置为"en",我 测试只有style8的时候才支持英文
Dim
url As String
If
style = 7 OrElse style = 8 Then
    url =
"https://webrd0{0}.is.autonavi.com/appmaptile?lang={1}&size={2}&scale={3}&style={4}&x={5}&y={6}&z={7}&scl={8}"
Else

    url =
"https://webst0{0}.is.autonavi.com/appmaptile?lang={1}&size={2}&scale={3}&style={4}&x={5}&y={6}&z={7}&scl={8}"
End
If
e.URI = CExp(url, server, Language, Size, scale, style, e.X, e.Y, e.Z, scl)

3、将窗口的AfterLoad事件代码设置为:

Dim map As GeoMap = e.Form.Controls("Map1").GeoMap
Dim
layer As New VectorLayer() '定义矢量层
layer.LabelVisibility = LabelVisibility.Visible
'标题可见
layer.Style.Stroke.Color = Color.Red
'标记边框为红色
layer.LabelStyle.ForeColor = Color.Green
'标记标题为绿色
map.Layers.Add(layer)
'将矢量层增加代map控件中
'
定义各城市的经纬度
Dim
cities As New Dictionary(Of String, GeoPoint)
cities.Add(
"北京", New GeoPoint(116.4053, 39.905))
cities.Add(
"上海", New GeoPoint(121.4726, 31.2317))
cities.Add(
"广州", New GeoPoint(113.2806, 23.1252))
cities.Add(
"成都", New GeoPoint(104.0657, 30.6595))
cities.Add(
"哈尔滨", New GeoPoint(126.6425, 45.757))
'
逐个绘制城市
For
Each city As String In cities.Keys
   
Dim mark As New VectorPlacemark() '增加一个标记
    mark.Marker.Caption = city
'指定标记的 标题
    mark.Geometry = cities(city)
'设置标记 位置为城市的经纬度
   
If city = "北京" Then '如果是北京
        mark.Marker.Size =
New SizeF(16, 16) '指定标记大小
        mark.Marker.Shape = MarkerShape.Star
'标记形状为五角星
   
Else
        mark.Marker.Size =
New SizeF(10, 10) '其他城市的标记要小一些
        mark.Marker.Shape = MarkerShape.Circle
'且标记 形状为圆形
   
End If
    mark.Marker.LabelPosition = LabelPosition.Right
'标题显示在标记右边
    mark.Style.BackColor = Color.Red
'标记的填充颜色为红色
    layer.Items.Add(mark)
'将标记添加到矢量层中
Next

'
绘制北京和各城市之间的链接线条
For
Each city As String In cities.Keys
   
If city <> "北京" Then
       
Dim points() As GeoPoint = {cities("北京"), cities(city)} '线条至少需要用两个 点,起点为北京的经纬度
       
Dim line As VectorPolyline = map.CreateLine(points) '创建线条
        line.Style.Stroke.Color = Color.Black
'颜色
        line.Style.Stroke.Style = GeoDashStyle.Dash
'线型
        layer.Items.Add(line)
'将线条添加到矢量层
   
End If
Next

线条可以包括多条线段

线条的类型为VectorPolyline。

通常一个
VectorPolyline对象对应一 个线段,但如果有必要,一个VectorPolyline对象可以包括多条不连续的线段。

例如将AfterLoad事件代码改为:

Dim map As GeoMap = e.Form.Controls("Map1").GeoMap
Dim
layer As New VectorLayer() '定义矢量层
layer.LabelVisibility = LabelVisibility.Visible
'标题可见
layer.Style.Stroke.Color = Color.Red
'标记变宽为红色
layer.LabelStyle.ForeColor = Color.Green
'标记标题为绿色
map.Layers.Add(layer)
'将矢量层增加代map控件中
'
定义各城市的经纬度
Dim
cities As New Dictionary(Of String, GeoPoint)
cities.Add(
"北京", New GeoPoint(116.4053, 39.905))
cities.Add(
"上海", New GeoPoint(121.4726, 31.2317))
cities.Add(
"广州", New GeoPoint(113.2806, 23.1252))
cities.Add(
"成都", New GeoPoint(104.0657, 30.6595))
cities.Add(
"哈尔滨", New GeoPoint(126.6425, 45.757))
'
创建线段数组
Dim
linePoints As New List(Of GeoPoint()) '一个GeoPoint数组集合。
linePoints.Add({cities(
"上海"), cities("北京")}) '定义第一条线经过的点
linePoints.Add({cities(
"成都"), cities("广州"), cities("上海"), cities("哈尔滨")}) '定义第二条线经过的点
Dim line As VectorPolyline = map.CreateLine(linePoints) '创建 的VectorPolyline包括两条线
line.Style.Stroke.Color = Color.Black
'颜色
line.Style.Stroke.Style = GeoDashStyle.Dash
'线型
layer.Items.Add(line)

'
逐个绘制城市
For
Each city As String In cities.Keys
   
Dim mark As New VectorPlacemark() '增加一个标记
    mark.Marker.Caption = city
'指定 标记的标题
    mark.Geometry = cities(city)
'设置标记位置为城市的经纬度
   
If city = "北京" Then '如果是北京
        mark.Marker.Size =
New SizeF(16, 16) '指定标记大小
        mark.Marker.Shape = MarkerShape.Star
'标记为五角星
   
Else
        mark.Marker.Size =
New SizeF(10, 10) '其他城市的标记要小一些
        mark.Marker.Shape = MarkerShape.Circle
'且标记为圆形
   
End If
    mark.Marker.LabelPosition = LabelPosition.Right
'标题显示在标记右边
    mark.Style.BackColor = Color.Red
'标记的填充颜色为红色
    layer.Items.Add(mark)
'将标记添加到矢量层中
Next

得到的地图如下,图中所有线条属于同一个对象:


本页地址:http://www.foxtable.com/webhelp/topics/6137.htm