如果您熟悉Visual Studio编程且了解在一般情况下如何使用C1Maps,请直接参考任务帮助,如果您是初次使用WPF及及
Silverlight版版Maps产品,请先参阅Silverlight及及WPF版版C1Maps快速入门快速入门。
在使用WPF及Silverlight版Maps产品时,每个任务帮助主题都提供一个特定任务的解决方案。
在您参阅任务帮助主题前,请确定您已经创建了一个新的WPF或Silverlight项目。
添加一个标签
在本主题中,通过使用C1VectorLayer及及C1VectorPlacemark,您将添加一个地理标签—宾夕法尼亚州伊利市(美国)地理坐标。了解更多矢量层相关信息,请参阅矢量层矢量层。
XAML
完成以下步骤:
将下述XAML标签添加至<c1:C1Maps>与</c1:C1Maps>之间:
XMAL
<c1:C1VectorLayer>
<c1:C1VectorPlacemark LabelPosition="Left" GeoPoint="-80.107008,42.16389" StrokeThickness="2" Foreground="#FFEB1212" PinPoint="-80.010866,42.156831"
Label="Erie, PA"/>
</c1:C1VectorLayer>- 运行项目。
代码代码
- 在XAML视图的<c1:C1Maps>标签中添加x:Name="C1Maps1", 这样项目则拥有一个在代码中被调用的唯一标识符。
在代码框中输入或导入下述命名空间:
Visual Basic
Imports C1.Silverlight.C1Maps
C#
using C1.Silverlight.C1Maps;
在 InitializeComponent()方法下添加下述代码:
Visual Basic
' 创建一个层并添加到地图中
Dim vl As C1VectorLayer = New C1VectorLayer()
C1Maps1.Layers.Add(vl)
' 创建一个矢量标记并添加到层中
Dim vp1 As C1VectorPlacemark = New C1VectorPlacemark() vl.Children.Add(vp1)' 为矢量标记设置一组地理坐标
vp1.GeoPoint = New Point(-80.107008, 42.16389)
' 设置标记的标签和属性
vp1.Label = "Erie, PA" vp1.FontSize = 12 vp1.Foreground = New SolidColorBrush(Colors.Red) vp1.LabelPosition = LabelPosition.CenterC#
//创建一个层并添加到地图中
C1VectorLayer vl = new C1VectorLayer();
C1Maps1.Layers.Add(vl);
//创建一个矢量标记并添加到层中
C1VectorPlacemark vp1 = new C1VectorPlacemark(); vl.Children.Add(vp1);
//为矢量标记设置一组地理坐标
vp1.GeoPoint = new Point(-80.107008, 42.16389);
//设置标记的标签和属性
vp1.Label = "Erie, PA"; vp1.FontSize = 12; vp1.Foreground = new SolidColorBrush(Colors.Red); vp1.LabelPosition = LabelPosition.Center;- 运行项目。
本主题示例如下图本主题示例如下图: 下方例图显示了使用C1Maps控件标记出宾夕法尼亚州伊利市(美国)的地理坐标。
添加一条折线
通过往C1VectorLayer中添加一个C1VectorPolyline(更多相关信息,请参阅矢量层矢量层),您可以以一条折线连接几个地理坐标,在本主题中,您使用XAML及代码创建一个包含三个点的折线。
XAML
完成如下步骤:
将下述XAML标签置于<c1:C1Maps>与</c1:C1Maps>之间:
C#
<c1:C1VectorLayer Margin="2,0,-2,0">
<c1:C1VectorPolyline Points="-80.15,42.12 -123.08,39.09, -3.90,30.85"
StrokeThickness="3" Stroke="Red">
</c1:C1VectorPolyline>
</c1:C1VectorLayer>- 按F5键运行项目。
代码代码
完成如下步骤:
- 在XAML视图的<c1:C1Maps>标签中添加x:Name="C1Maps1", 这样项目则拥有一个在代码中被调用的唯一标识符。
在代码框中输入和导入下述命名空间:
Visual Basic
Imports C1.Silverlight.C1Maps
C#
using C1.Silverlight.C1Maps;
Visual Basic
' 创建一个层并添加到地图中
Dim C1VectorLayer1 As New C1VectorLayer()
C1Maps1.Layers.Add(C1VectorLayer1)
' 初始化轨迹
Dim pts As Point() = New Point() {New Point(-80.15, 42.12), New Point(-123.08,
39.09), New Point(-3.9, 30.85)}
' 创建一个集合并将坐标点添加到集合中
Dim pcoll As New PointCollection()
For Each pt As Point In pts pcoll.Add(pt) Next
' 创建一条折线,并添加到矢量层中
Dim C1VectorPolyline1 As New C1VectorPolyline()
C1VectorLayer1.Children.Add(C1VectorPolyline1)' 设置坐标点
C1VectorPolyline1.Points = pcoll
' 设置外观
C1VectorPolyline1.Stroke = New SolidColorBrush(Colors.Red) C1VectorPolyline1.StrokeThickness = 3在InitializeComponent()方法下添加下述代码:
C#
//创建一个层并添加到地图中
C1VectorLayer C1VectorLayer1 = new C1VectorLayer(); c1Maps1.Layers.Add(C1VectorLayer1);
//初始化轨迹
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ad2857e9-4b86-4ddf-bf37-0a7c43446119"><ac:plain-text-body><![CDATA[Point[] pts = new Point[] { new Point(-80.15,42.12), new Point(-123.08,39.09), new Point(-3.90,30.85)};
]]></ac:plain-text-body></ac:structured-macro>
//创建一个集合并将坐标点添加到集合中
PointCollection pcoll = new PointCollection(); foreach( Point pt in pts) pcoll.Add(pt);
//创建一条折线,并添加到矢量层中
C1VectorPolyline C1VectorPolyline1 = new C1VectorPolyline(); v1.Children.Add(C1VectorPolyline1);
//设置坐标点
C1VectorPolyline1.Points = pcoll;
//设置外观
C1VectorPolyline1.Stroke = new SolidColorBrush(Colors.Red);
C1VectorPolyline1.StrokeThickness = 3;- 按F5键运行项目。
本主题示例如下图:本主题示例如下图:下图描绘了使用C1Maps控件将三个地理坐标点用一条折线连接起来。
添加一个多边形
通过往C1VectorLayer中添加一个C1VectorPolygon(更多相关信息,请参阅矢量层矢量层),您可以以一个多边形连接几个地理坐标,在本主题中,您使用XAML及代码创建一个包含三个点的多边形。
XAML
完成如下步骤:
将下述XAML标签置于<c1:C1Maps>与</c1:C1Maps>之间:
XAML
<c1:C1VectorLayer Margin="2,0,-2,0">
<c1:C1VectorPolygon Points="-80.15,42.12 -123.08,39.09, -3.90,30.85"
StrokeThickness="3" Stroke="Red">
</c1:C1VectorPolygon>
</c1:C1VectorLayer>- 按F5键运行项目。
代码代码
完成如下步骤:
- 在XAML视图的<c1:C1Maps>标签中添加x:Name="C1Maps1", 这样项目则拥有一个在代码中被调用的唯一标识符。
在代码框中输入或导入下述命名空间:
Visual Basic
Imports C1.Silverlight.C1Maps
C#
using C1.Silverlight.C1Maps;
在InitializeComponent()方法下添加下述代码:
Visual Basic
' 创建一个层并添加到地图中
Dim C1VectorLayer1 As New C1VectorLayer()
C1Maps1.Layers.Add(C1VectorLayer1)
' 初始化轨迹
Dim pts As Point() = New Point() {New Point(-80.15, 42.12), New Point(-123.08,
39.09), New Point(-3.9, 30.85)}
' 创建一个集合并将坐标点添加到集合中
Dim pcoll As New PointCollection()
For Each pt As Point In pts pcoll.Add(pt)
Next' 创建一条折线,并添加到矢量层中
Dim C1VectorPolygon1 As New C1VectorPolygon()
C1VectorLayer1.Children.Add(C1VectorPolygon1)
' 设置坐标点
C1VectorPolygon1.Points = pcoll
' 设置外观
C1VectorPolygon1.Stroke = New SolidColorBrush(Colors.Red) C1VectorPolygon1.StrokeThickness = 3C#
//创建一个层并添加到地图中
C1VectorLayer C1VectorLayer1 = new C1VectorLayer();
C1Maps1.Layers.Add(C1VectorLayer1);
//初始化轨迹
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9e74b78a-a90a-410f-bf5f-b0be43ff53cc"><ac:plain-text-body><![CDATA[Point[] pts = new Point[] { new Point(-80.15,42.12), new Point(-123.08,39.09), new Point(-3.90,30.85)};
]]></ac:plain-text-body></ac:structured-macro>
//创建一个集合并将坐标点添加到集合中
PointCollection pcoll = new PointCollection(); foreach( Point pt in pts) pcoll.Add(pt);
//创建一条折线,并添加到矢量层中
C1VectorPolygon C1VectorPolygon1 = new C1VectorPolygon(); v1.Children.Add(C1VectorPolygon1);
//设置坐标点
C1VectorPolygon1.Points = pcoll;
//设置外观
C1VectorPolygon1.Stroke = new SolidColorBrush(Colors.Red);
C1VectorPolygon1.StrokeThickness = 3;- 按F5键运行项目。
本主题示例如下图:本主题示例如下图:下图描绘了使用C1Maps控件将三个地理坐标点用一个多边形连接起来。
鼠标悬停地理坐标显示
在本主题中,您通过在项目中添加相应的代码,即可返回当前鼠标位置的地理坐标,这些地理坐标最终可以被写入TextBox控件中的Text属性。
为得到当前鼠标位置的地理坐标,需完成下列步骤:
- 添加一个StackPanel,TextBox 及C1Maps控件到您的项目中。
- 在Objects and Timeline面板中,重新排列控件,其显示如下图所示:
3. 按下述设置*StackPanel*属性:定位*Width*属性并点击图形符定位*Height*属性并点击图形符 ,设置Width属性为Auto。 ,设置Height属性为Auto.
- 设置TextBox控件的Name属性为"ShowCoordinates"。
- C1Maps control的属性设置如下: Width属性设置为"350"。
Height属性设置为"250"。
- 在Properties面板中选择C1Maps控件,并点击Events按钮。
- 在MouseMove文本框中,输入"MouseMoveCoordinates"并按ENTER键将MouseMoveCoordinates事件处理加入到您的项目中。
将代码注释用下述代码替换:
Visual Basic
Dim map As C1Maps = TryCast(sender, C1Maps)
Dim p As Point = map.ScreenToGeographic(e.GetPosition(map))ShowCoordinates.Text = String.Format("{0:f6},{1:f6}", p.X, p.Y)
C#
C1Maps map = sender as C1Maps;
Point p = map.ScreenToGeographic(e.GetPosition(map));
ShowCoordinates.Text = string.Format("{0:f6},{1:f6}", p.X, p.Y);导入下述命名空间:
Visual Basic
Imports C1.WPF.C1Maps
C#
using C1.WPF.C1Maps;
- 按F5键运行项目,一旦项目被载入,您可以看到当鼠标的光标在地图上悬停时,在文本框中会显示相应的地理坐标。
重新排列Map工具
您可以通过C1MapToolsLayer和模板来更改地图工具,了解更多详细信息,请参阅自定义工具自定义工具。请完成如下步骤:
- 选择C1Maps来显示Properties面板中的属性列表。
- 清空Show Tools 中的复选框,此时会隐藏所有默认工具。
- 点击Layers (Collection)省略号按钮, 此时IMapLayer集合编辑器中Layers 对话框将被打开。
- 点击Add another item打开Select Object 对话框。
- 选择C1MapToolsLayer并点击OK来关闭Select Object 对话框。
在Objects and Timeline面板中,右键点击[C1MapToolsLayer]并选择Edit Template | Edit a Copy。将模板命名为"Tutorial Template"并点击OK。
此时您创建了一个新模板,观察下图中Objects and Timeline标签有一个父网格和两个子网格。
- 点击Child Grid 1 可以在设计视图中显示它所表示的网格。
- 在设计视图中,使用光标将已被选中的网格移动到地图的右侧,此时您的项目会与下图相似:
- 在Objects and Timeline面板中,点击Child Grid 2 可以在设计视图中显示它所表示的网格。
- 在设计视图中,使用光标将已被选中的网格移动到地图的左上方,此时您的项目会与下图相似:
- 按F5键运行项目,此时C1Maps控件显示如下:
更改地图资源
C1Maps可以从不同的资源中显示地理信息,默认情况下,C1Maps采用Microsoft LiveMaps航拍图片作为资源,但您可以通过Source属性对资源类型做出更改,它是一个MultiScaleTileSource类型的对象。在默认情况下,它被设置为显示Bing Map(在使用此项服务之前请参阅法律要求法律要求)航拍图片,但您可以更改显示路拍资源或混合资源。
更改为路拍资源更改为路拍资源完成下述步骤:
- 在XAML视图的<c1:C1Maps>标签中添加x:Name="C1Maps1", 这样项目则拥有一个在代码中被调用的唯一标识符。
在代码框中输入或导入下述命名空间:
Visual Basic
Imports C1.Silverlight.C1Maps
C#
using C1.Silverlight.C1Maps;
在InitializeComponent()方法下添加下述代码:
Visual Basic
C1Maps1.Source = new VirtualEarthRoadSource()
C#
C1Maps1.Source = new VirtualEarthRoadSource();
- 按F5键运行程序,可以在下图中显示路拍地图资源。
更改为混合资源更改为混合资源完成以下步骤:
- 在XAML视图的<c1:C1Maps>标签中添加x:Name="C1Maps1", 这样项目则拥有一个在代码中被调用的唯一标识符。
在代码框中输入或导入下述命名空间:
Visual Basic
Imports C1.Silverlight.C1Maps
C#
using C1.Silverlight.C1Maps;
在InitializeComponent()方法下添加下述代码:
Visual Basic
C1Maps1.Source = new VirtualEarthHybridSource()
C#
C1Maps1.Source = new VirtualEarthHybridSource();
- 按F5键运行程序,可以在下图中显示混合地图资源。
了解更多地图资源的详细信息,请参阅C1Maps概念和主要属性概念和主要属性。