地图定位实现,地图平移自动
问题1:当点击某一条数据时将实现地图定位功能,但要如何才能控制定位的缩放呢,按几何对象的大小缩放不是很好,今天特意去观察了下百度,发现百度地图中不管几何对象都大,定位时都是控制了地图的缩放的,故本人实现如下
1 /// <summary> 2 /// 使用矩形对象获取定位矩形对象 3 /// </summary> 4 /// <param name="enveLope">矩形实例</param> 5 /// <param name="dScale">定位时,定位信息X、Y最大最小值所占屏幕宽度比例,取值范围为0-1(不包含0包含1,否则将返回原矩形对象) </param> 6 /// <param name="mindistance">如果矩形实例中xy的最大差值小于这个值,定位将采用这个默认的xy差值进行缩放,默认值为60.0</param> 7 /// <returns></returns> 8 public static Geometry GetZoomToGeometry(Envelope enveLope, double dScale, double mindistance) 9 { 10 if (mindistance <= 0.0) mindistance = 60.0; 11 if (dScale <= 0.0 || dScale > 1.0) return enveLope; 12 double widthX = enveLope.XMax - enveLope.XMin, hightY = enveLope.YMax - enveLope.YMin; 13 if (widthX < mindistance && hightY < mindistance) 14 { 15 MapPoint minPoint = new MapPoint(enveLope.GetCenter().X - mindistance, enveLope.GetCenter().Y - mindistance); 16 MapPoint maxPoint = new MapPoint(enveLope.GetCenter().X + mindistance, enveLope.GetCenter().Y + mindistance); 17 return new Envelope(minPoint, maxPoint); 18 } 19 else 20 { 21 double xVaue = (widthX / 2.0) / dScale; double yValue = (hightY / 2.0) /dScale; 22 MapPoint minPoint = new MapPoint(enveLope.GetCenter().X - xVaue, enveLope.GetCenter().Y - yValue); 23 MapPoint maxPoint = new MapPoint(enveLope.GetCenter().X + xVaue, enveLope.GetCenter().Y + yValue); 24 return new Envelope(minPoint, maxPoint); 25 } 26 }
问题2:在百度地图测距中当鼠标将移出地图时,地图实现自动平移,代码实现
1 /// <summary> 2 /// 增加 地图测量时 移至地图边缘时 地图能够相应进行平移操作 3 /// </summary> 4 private void BindMeasureMapMouseMoveAction() 5 { 6 //设置 绑定地图控件的 鼠标移动事件 7 this.MapMainMap.MouseMove += (sender, e) => 8 { 9 if (!IsMeasureAction) 10 return; 11 Point tmpoint = e.GetPosition(this.MapMainMap);//获取当前鼠标坐标点 12 //获取 距离左测边框 和距离上册边框的 距离 以及 上下左右边框的高和宽 13 double curWidth = this.MapMainMap.ActualWidth, curHeight = this.MapMainMap.ActualHeight, mouseWidth = tmpoint.X, mouseHeight = tmpoint.Y; 14 if (curWidth - mouseWidth < 30 || mouseWidth < 30)//距离左侧过近 或右侧过近 进行平移 15 { 16 Point tmx = new Point { X = curWidth / 2 + (mouseWidth < 30 ? -50 : 50), Y = curHeight / 2 };//计算平移后中心点距离 17 this.MapMainMap.PanTo(this.MapMainMap.ScreenToMap(tmx));//执行平移 18 } 19 else if (curHeight - mouseHeight < 30 || mouseHeight < 30)//距离上侧过近 或下侧过近 进行平移 20 { 21 Point tmx = new Point { X = curWidth / 2, Y = curHeight / 2 + (mouseHeight < 30 ? -50 : 50) };//计算平移后中心点距离 22 this.MapMainMap.PanTo(this.MapMainMap.ScreenToMap(tmx));//执行平移 23 } 24 }; 25 }