前阵做一个项目过程中,需要结合MapInfo进行地方展示开发,积累一点点心得与大家共享
以下所有基于MapXtreme 6.6
MapInfo提供了足够强大的WinForm地图空间,可以很方便地在VS中拖拉摆放。
但如此强大的控件也带来了封闭性太强的问题。
现在我们都接触到了大量Web上的地图,比如Google Map,Live Map等,这些Web地图都具有非常高的交互能力,缩放由鼠标双击或鼠标中键滚动,或者使用缩放工具完成,但换到WinForm上,这些非常好的交互能力全都没有了,变成了一个个冰冷的控件(如下图),需要手动触发才能调用相应功能,这样的功能给用户多大的困扰啊!
需要改变一下了:
首先在需要使用MapControl的窗体构造一个MapControl对象,并声明如下事件:
缩放等级改变事件
1 /**//// <summary>
2 /// 缩放等级改变事件
3 /// </summary>
4 /// <remarks>在本地图的缩放等级发生变化后触发</remarks>
5 public event EventHandler ZoomLevelChanged;
1 /**//// <summary>
2 /// 缩放等级改变事件
3 /// </summary>
4 /// <remarks>在本地图的缩放等级发生变化后触发</remarks>
5 public event EventHandler ZoomLevelChanged;
这个事件的使用如下所示:
Code
/**//// <summary>
/// 设置地图显示比例
/// </summary>
/// <remarks>窗体上所有缩放事件均进入该方法进行</remarks>
/// <param name="ZoomLevel"></param>
public void SetZoomLevel (int ZoomLevel)
{
_helper.Zoom ( ZoomLevel );
_zoomLevel = _helper.ZoomLevel;
if ( ZoomLevelChanged != null )
{
//触发事件
ZoomLevelChanged ( this, new MapControlEventArg ( _zoomLevel ) );
}
}
/**//// <summary>
/// 设置地图显示比例
/// </summary>
/// <remarks>窗体上所有缩放事件均进入该方法进行</remarks>
/// <param name="ZoomLevel"></param>
public void SetZoomLevel (int ZoomLevel)
{
_helper.Zoom ( ZoomLevel );
_zoomLevel = _helper.ZoomLevel;
if ( ZoomLevelChanged != null )
{
//触发事件
ZoomLevelChanged ( this, new MapControlEventArg ( _zoomLevel ) );
}
}
声明ZoomLevelChanged事件时,为了省事,没有使用相应的委托来做,其实在EventArg处,我使用了一个继承自EventArg的类,并把当前缩放等级值传入其中。
准备工作完成了,那么现在是时候构造地图缩放工具了。
设计思路是这样的:
由于缩放地图具有一定的范围,那么我就把地图的缩放设置为8个等级,并对应MapInfo的缩放距离,该缩放方法使用一个外部类控制:
设置地图缩放等级
/// <summary>
/// 设置地图缩放层级
/// </summary>
/// <param name="map">地图实例引用</param>
/// <param name="zoomLevel">缩放层级</param>
private void SetZoomLevel ( Map map,int zoomLevel )
{
try
{
map.Zoom = ExchangeZoomLevel ( zoomLevel );
}
catch
{ }
}
/// <summary>
/// 设置地图缩放层级
/// </summary>
/// <param name="map">地图实例引用</param>
/// <param name="zoomLevel">缩放层级</param>
private void SetZoomLevel ( Map map,int zoomLevel )
{
try
{
map.Zoom = ExchangeZoomLevel ( zoomLevel );
}
catch
{ }
}
点击该缩放控件时,调用的是上面的方法,并把地图控件中的地图引用传递过去,实现缩放功能。
由于MapInfo的MapControl控件已经提供了比较完善的API,使得缩放功能得以方便地实现。
构造缩放控件类,并在类中声明以下变量以供使用
声明变量
/// <summary>
/// 保存对父控件的引用
/// </summary>
private System.Windows.Forms.Control _control;
/// <summary>
/// 维护当前的等级Label
/// </summary>
private List<System.Windows.Forms.Label> levelLabelList;
/// <summary>
/// 控件左上角显示坐标X
/// </summary>
private readonly int xPosition = 10;
/// <summary>
/// 控件左上角显示坐标Y
/// </summary>
private readonly int oginYPosition = 10;
/// <summary>
/// 当前缩放等级Label颜色
/// </summary>
private System.Drawing.Color currentLevelLabelColor = System.Drawing.Color.Blue;
/// <summary>
/// 各控件的ToolTip
/// </summary>
System.Windows.Forms.ToolTip tt;
/// <summary>
/// 保存对父控件的引用
/// </summary>
private System.Windows.Forms.Control _control;
/// <summary>
/// 维护当前的等级Label
/// </summary>
private List<System.Windows.Forms.Label> levelLabelList;
/// <summary>
/// 控件左上角显示坐标X
/// </summary>
private readonly int xPosition = 10;
/// <summary>
/// 控件左上角显示坐标Y
/// </summary>
private readonly int oginYPosition = 10;
/// <summary>
/// 当前缩放等级Label颜色
/// </summary>
private System.Drawing.Color currentLevelLabelColor = System.Drawing.Color.Blue;
/// <summary>
/// 各控件的ToolTip
/// </summary>
System.Windows.Forms.ToolTip tt;
接下来构造控件的现实状态。由于UserControl或Control类生成出来的控件都会带有默认的矩形背景,这一点都不雅观,所以我还是采用了一个愚蠢方法,在MapControl上直接使用AddControl()。
构造缩放工具
1 /**//// <summary>
2 /// 构造缩放工具
3 /// </summary>
4 public void BuildZoomTool ( )
5 {
6 System.Windows.Forms.Control mc = ( (MapControl) _control ).mapControl;
7 System.Windows.Forms.Label labelZoomIn = CreateZoomInLabel ();
8 mc.Controls.Add ( labelZoomIn );
9 int yPosition = 32;
10 //循环次数与最大缩放等级有关
11 for ( int i = MapHelper.MinZoomLevel; i <= MapHelper.MaxZoomLevel; i++, yPosition += 15 )
12 {
13 System.Windows.Forms.Label levelLabel = CreateLevelLabel ( i, yPosition+oginYPosition );
14 levelLabelList.Add ( levelLabel );
15 mc.Controls.Add ( levelLabel );
16 }
17 System.Windows.Forms.Label labelZoomOut = CreateZoomOutLabel ( yPosition+oginYPosition );
18 mc.Controls.Add ( labelZoomOut );
19 }
20
1 /**//// <summary>
2 /// 构造缩放工具
3 /// </summary>
4 public void BuildZoomTool ( )
5 {
6 System.Windows.Forms.Control mc = ( (MapControl) _control ).mapControl;
7 System.Windows.Forms.Label labelZoomIn = CreateZoomInLabel ();
8 mc.Controls.Add ( labelZoomIn );
9 int yPosition = 32;
10 //循环次数与最大缩放等级有关
11 for ( int i = MapHelper.MinZoomLevel; i <= MapHelper.MaxZoomLevel; i++, yPosition += 15 )
12 {
13 System.Windows.Forms.Label levelLabel = CreateLevelLabel ( i, yPosition+oginYPosition );
14 levelLabelList.Add ( levelLabel );
15 mc.Controls.Add ( levelLabel );
16 }
17 System.Windows.Forms.Label labelZoomOut = CreateZoomOutLabel ( yPosition+oginYPosition );
18 mc.Controls.Add ( labelZoomOut );
19 }
20
以下是构造Label的方法:
构造各Label
1 private System.Windows.Forms.Label CreateLevelLabel ( int zoomLevel,int yPosition )
2 {
3 System.Windows.Forms.Label label = new System.Windows.Forms.Label ();
4 label.BackColor = System.Drawing.Color.White;
5 label.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
6 label.Font = new System.Drawing.Font ( "宋体", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (byte) ( 134 ) ) );
7 label.Text = " ";
8 label.Name = "lblLevel" + zoomLevel.ToString ();
9 label.Tag = zoomLevel;
10 label.Cursor = System.Windows.Forms.Cursors.Hand;
11 label.Location = new System.Drawing.Point ( xPosition, yPosition );
12 label.Size = new System.Drawing.Size ( 37, 11 );
13 label.Click += new EventHandler ( label_Click );
14 tt = new System.Windows.Forms.ToolTip ();
15 tt.ShowAlways = true;
16 tt.SetToolTip ( label, "缩放地图到"+zoomLevel.ToString()+"级" );
17 return label;
18 }
19
1 private System.Windows.Forms.Label CreateLevelLabel ( int zoomLevel,int yPosition )
2 {
3 System.Windows.Forms.Label label = new System.Windows.Forms.Label ();
4 label.BackColor = System.Drawing.Color.White;
5 label.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
6 label.Font = new System.Drawing.Font ( "宋体", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (byte) ( 134 ) ) );
7 label.Text = " ";
8 label.Name = "lblLevel" + zoomLevel.ToString ();
9 label.Tag = zoomLevel;
10 label.Cursor = System.Windows.Forms.Cursors.Hand;
11 label.Location = new System.Drawing.Point ( xPosition, yPosition );
12 label.Size = new System.Drawing.Size ( 37, 11 );
13 label.Click += new EventHandler ( label_Click );
14 tt = new System.Windows.Forms.ToolTip ();
15 tt.ShowAlways = true;
16 tt.SetToolTip ( label, "缩放地图到"+zoomLevel.ToString()+"级" );
17 return label;
18 }
19
以上需要注意的是,我把该控件所关联的缩放等级设置到控件的Tag中,并注册了点击事件,那么在该事件中,我们只需要把该控件所保留的缩放等级设置到地图上就可以了:
Label事件
1 void label_Click ( object sender, EventArgs e )
2 {
3 System.Windows.Forms.Label label = (System.Windows.Forms.Label) sender;
4 MapControl mc = (MapControl) _control;
5 mc.SetZoomLevel ( Int32.Parse ( label.Tag.ToString () ) );
6 }
1 void label_Click ( object sender, EventArgs e )
2 {
3 System.Windows.Forms.Label label = (System.Windows.Forms.Label) sender;
4 MapControl mc = (MapControl) _control;
5 mc.SetZoomLevel ( Int32.Parse ( label.Tag.ToString () ) );
6 }
如此以上,我们的地图缩放工具就完成了。
当然,有了这个工具还是不够的,所以我又在MapControl上注册了鼠标双击事件,由双击鼠标来进行地图的缩放:
MapControl双击事件
1 public void mc_Map_MouseDoubleClick ( object sender, MouseEventArgs e )
2 {
3 switch ( e.Button )
4 {
5 case MouseButtons.Left:
6 ZoomIn ();
7 break;
8 case MouseButtons.Right:
9 ZoomOut ();
10 break;
11 }
12 _helper.SetCenter ( e.Location );
13 }
14
1 public void mc_Map_MouseDoubleClick ( object sender, MouseEventArgs e )
2 {
3 switch ( e.Button )
4 {
5 case MouseButtons.Left:
6 ZoomIn ();
7 break;
8 case MouseButtons.Right:
9 ZoomOut ();
10 break;
11 }
12 _helper.SetCenter ( e.Location );
13 }
14
这是最后的效果,当点击任意的缩放等级,地图就会缩放到适当的大小