問題1:當點擊某一條數據時將實現地圖定位功能,但要如何才能控制定位的縮放呢,按幾何對象的大小縮放不是很好,今天特地去觀察了下百度,發現百度地圖中無論幾何對象都大,定位時都是控制了地圖的縮放的,故本人實現以下ide
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:在百度地圖測距中當鼠標將移出地圖時,地圖實現自動平移,代碼實現this
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 }