欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Gmap实现旋转原理以及代码

程序员文章站 2024-03-04 12:22:53
...
原理:按下鼠标的时候(开始移动)计算校准值,后续的移动中都减去这个校准值。例如当前旋转角度为10,当前按下鼠标点开始移动的旋转角度为30,那么校准值为30-10=20,意思就是从角度为30这个点开始都要减去20让旋转角度值从10开始,不能跳动。后续的持续按下鼠标右键移动中都使用鼠标Location点的角度减去这个校准值作为新的旋转角度值。
即:bearing=newAngel-step;
下面是实现的一些列事件和方法:
getAngle方法计算某点对于中心点的角度。
按下鼠标时计算当前点和当前角度的差值作为校准值:
移动鼠标事件实现旋转:总是将当前点的角度减去校准值作为新的旋转角度:
//自己注意处理注册事件gMap为我的地图控件:
//gMap.MouseMove += gMap_MouseMove;
//gMap.MouseDoubleClick += gMap_MouseDoubleClick;
//gMap.MouseDown += gMap_MouseDown;
//gMap.MouseUp += gMap_MouseUp;

private static object objForLock__step = new object();
        private double __step = 0;
        private double _step //校准值
        {
            get { lock (objForLock__step) { return __step; } }
            set { lock (objForLock__step) { __step = value; } }
        }

private void gMap_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                var pCenter = gMap.FromLatLngToLocal(gMap.Position);
                var center = new Point((int)pCenter.X, (int)pCenter.Y);
                _step = getAngle(e.Location, center)-gMap.Bearing;gMap.Cursor = Cursors.Hand;
                debug("gMap_MouseDown:_step=" + _step.ToString());
            }
        }
private void gMap_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                var pCenter = gMap.FromLatLngToLocal(gMap.Position);
                var center = new Point((int)pCenter.X, (int)pCenter.Y);
                //
                var bearing = gMap.Bearing = (float)(getAngle(e.Location, center) -_step);
                debug("gMap_MouseMove:" + bearing);
            }
        }

/// <summary>
        /// 计算旋转角度的相关函数
        /// </summary>
        /// <param name="nowpoint"></param>
        /// <returns></returns>
        public double getAngle(Point nowpoint, Point CentPoint)
        {
            double length = PointLegth(nowpoint, CentPoint);

            double hudu = Math.Asin(Math.Abs(nowpoint.Y - CentPoint.Y) / length);
            double ag = hudu * 180 / Math.PI;

            if ((CentPoint.X - nowpoint.X) <= 0 && (CentPoint.Y - nowpoint.Y) >= 0)
            {
                ag = 90 - ag;
            }
            else
            {
                if ((CentPoint.X - nowpoint.X) <= 0 && (CentPoint.Y - nowpoint.Y) <= 0)
                {
                    ag = ag + 90;
                }
                else
                {
                    if ((CentPoint.X - nowpoint.X) >= 0 && (CentPoint.Y - nowpoint.Y) <= 0)
                    {
                        ag = 270 - ag;
                    }
                    else
                    {
                        if ((CentPoint.X - nowpoint.X) >= 0 && (CentPoint.Y - nowpoint.Y) >= 0)
                        {
                            ag = ag + 270;
                        }
                    }
                }
            }
            ag -= 235;
            return 360-ag;
        }

        /// <summary>
        /// 计算两点间距离
        /// </summary>
        /// <param name="pa"></param>
        /// <param name="pb"></param>
        /// <returns></returns>
        public double PointLegth(Point pa, Point pb)
        {
            return Math.Sqrt(Math.Pow((pa.X - pb.X), 2) + Math.Pow((pa.Y - pb.Y), 2));
        }
相关标签: C# GMap 旋转