用AE实现google earth的导航与跟踪条控制地图比例

在坛子上问了两天,结果没有人回答,心寒,没办法,靠自己。。嘿嘿,最后竟然也让我解决了。。开心一个。

下面把代码贴一下。

 

首先是地图向上,向下,向左,向右代码的实现:

 private void btright_Click(object sender, EventArgs e)
        {
            IEnvelope pEnv = new EnvelopeClass();
            double Xcenter = axMapControl.Extent.XMax - axMapControl.Extent.Width * 0.4;
            double Ycenter = axMapControl.Extent.YMax - axMapControl.Extent.Height / 2;
            IPoint pPoint = new PointClass();
            pPoint.X = Xcenter;
            pPoint.Y = Ycenter;
            axMapControl.CenterAt(pPoint);
        }

        private void btleft_Click(object sender, EventArgs e)
        {
            IEnvelope pEnv = new EnvelopeClass();
            double Xcenter = axMapControl.Extent.XMax - axMapControl.Extent.Width * 0.6;
            double Ycenter = axMapControl.Extent.YMax - axMapControl.Extent.Height / 2;
            IPoint pPoint = new PointClass();
            pPoint.X = Xcenter;
            pPoint.Y = Ycenter;
            axMapControl.CenterAt(pPoint);
         
        }

        private void btdown_Click(object sender, EventArgs e)
        {
            IEnvelope pEnv = new EnvelopeClass();
            double Xcenter = axMapControl.Extent.XMax - axMapControl.Extent.Width / 2;
            double Ycenter = axMapControl.Extent.YMax - axMapControl.Extent.Height * 0.6;
            IPoint pPoint = new PointClass();
            pPoint.X = Xcenter;
            pPoint.Y = Ycenter;
            axMapControl.CenterAt(pPoint);
        }

        private void btup_Click(object sender, EventArgs e)
        {
            IEnvelope pEnv = new EnvelopeClass();
            double Xcenter = axMapControl.Extent.XMax - axMapControl.Extent.Width / 2;
            double Ycenter = axMapControl.Extent.YMax - axMapControl.Extent.Height * 0.4;
            IPoint pPoint = new PointClass();
            pPoint.X = Xcenter;
            pPoint.Y = Ycenter;
            axMapControl.CenterAt(pPoint);
        }

其实原理是很简单的,向上的话,就是把地图中心的Y值加大一定的数值。(写到这里的时候突然发现我的实现方法太麻烦了。我的实现方法是获取当前视图的最大X值和最大Y值,然后通过计算X,Y与新的地图中心X,Y值的关系,确定出新的地图中心的X,Y值,然后定一个点,把这个X,Y值赋给它,最后利用MapControl的CenterAt(IPoint)方法即可。)

 

接下来实现的是利用TrackBar工具,控制地图的比例尺变化。

由于mapControl支持滚轮缩放,所以还要考虑一下,当用户进行滚轮缩放,或者是利用zoomin,zoomout工具进行地图缩放操作时,TrackBar的value值也要相应的有变化。

        private void axMapControl_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
        {
            //限制地图的最大最小比例,因为滚轮能够将地图的比例无限放大或放小,对于实际应用不利。
            double nowscale = axMapControl.MapScale;
            if (nowscale > 800000)
            {
                axMapControl.MapScale = 800000;
            }
            if (nowscale < 100)
            {
                axMapControl.MapScale = 100;
            }
            if (e.sizeChanged == true)
            //地图比例尺变动时,跟踪条的值跟着变化,各个比例我是根据我的底图进行设定的。
            {
                if (nowscale <800)
                {
                    scalebar.Value = 10;

                }
                else if (nowscale >= 800 && nowscale < 1500)
                {
                    scalebar.Value = 9;
                }
                else if (nowscale >= 1500 && nowscale < 3000)
                {
                    scalebar.Value =8;
                }
                else if (nowscale >= 3000 && nowscale < 6500)
                {
                    scalebar.Value = 7;
                }
                else if (nowscale >= 6500 && nowscale < 20000)
                {
                    scalebar.Value = 6;
                }
                else if (nowscale >= 20000 && nowscale < 35000)
                {
                    scalebar.Value =5;
                }
                else if (nowscale >=35000 && nowscale < 75000)
                {
                    scalebar.Value =4;
                }
                else if (nowscale >= 75000 && nowscale <100000)
                {
                    scalebar.Value =3;
                }
                else if (nowscale >= 100000 && nowscale < 200000)
                {
                    scalebar.Value = 2;
                }
                else if (nowscale >= 200000 && nowscale < 300000)
                {
                    scalebar.Value = 1;
                }
                else if (nowscale >= 300000)
                {
                    scalebar.Value = 0;
                }
            }
        
            cbscale.Text = "1:"+axMapControl.MapScale.ToString("F2");//在combobox中显示出当前的地图比例尺
        }

 

以上就实现了地图比例变化时,跟踪条的值相应的变化。接下来要实现的,是控制跟踪条的值,来控制地图比例尺。

由于在上一个阶段中,地图比例尺变化时,会触动Trackbar的valuechangedg事件,而利用Trackbar来控制地图比例尺时,也是要在Trackbar的valuechanged事件中编写,为了避免干扰,所以设一个判断语句,当在Trackbar上发生鼠标点击事件时,才会触动“由跟踪条值变化控制地图比例尺变化”的代码。

   bool i;

   private void scalebar_MouseDown(object sender, MouseEventArgs e)
        {
            i = true;

        }

 private void axMapControl_OnMouseUp(object sender, IMapControlEvents2_OnMouseUpEvent e)
        {
            i = false;
        }

 

  private void scalebar_ValueChanged(object sender, EventArgs e)
        {
          if (i == true)
            {
                if (scalebar.Value == 0)
                {
                    axMapControl.MapScale = 300000;
                    axMapControl.ActiveView.Refresh();
                }
                else if (scalebar.Value == 1)
                {
                    axMapControl.MapScale = 200000;
                    axMapControl.ActiveView.Refresh();
                }
                else if (scalebar.Value == 2)
                {
                    axMapControl.MapScale = 100000;
                    axMapControl.ActiveView.Refresh();
                }
                else if (scalebar.Value == 3)
                {
                    axMapControl.MapScale = 75000;
                    axMapControl.ActiveView.Refresh();
                }
                else if (scalebar.Value == 4)
                {
                    axMapControl.MapScale = 35000;
                    axMapControl.ActiveView.Refresh();
                }
                else if (scalebar.Value == 5)
                {
                    axMapControl.MapScale = 20000;
                    axMapControl.ActiveView.Refresh();
                }
                else if (scalebar.Value == 6)
                {
                    axMapControl.MapScale = 10000;
                    axMapControl.ActiveView.Refresh();
                }
                else if (scalebar.Value == 7)
                {
                    axMapControl.MapScale = 5000;
                    axMapControl.ActiveView.Refresh();
                }
                else if (scalebar.Value == 8)
                {
                    axMapControl.MapScale = 2000;
                    axMapControl.ActiveView.Refresh();
                }
                else if (scalebar.Value == 9)
                {
                    axMapControl.MapScale = 1000;
                    axMapControl.ActiveView.Refresh();
                }
                else if (scalebar.Value ==10)
                {
                    axMapControl.MapScale = 800;
                    axMapControl.ActiveView.Refresh();
                }
              
            }
        }
以上就是整个事件的实现代码。写得很烦琐,肯定会有更精简的方法的,欢迎各位提出。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值