/// <summary> /// 右键菜单 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cm_popup_Click(object sender, EventArgs e) { ToolStripMenuItem item = sender as ToolStripMenuItem; if(item.Name == "cmsWhere") //这是哪里 { LatLngPoint location = MapHelper.GetLatLngByScreenLocation(_right_mouse_point_cache, _center, _zoom, ClientSize); ((Action)delegate() { GeocodingService gs = new GeocodingService(); JObject point = gs.DeGeocoding(location.Lat + "," + location.Lng); if (point != null) //具体json格式参见api文档 { this.Invoke((Action)delegate() { _current_selected_point = _theStrangePoint = new BPoint { Type = PointType.Strange, Selected = true, Address = (string)point["result"]["formatted_address"], Location = location }; _bPointTipControl.BPoint = _theStrangePoint; _bPointTipControl.Location = new Point(_right_mouse_point_cache.X - _bPointTipControl.Width / 3 + 37, _right_mouse_point_cache.Y - _bPointTipControl.Height - 34); _bPointTipControl.Visible = true; }); } }).BeginInvoke(null, null); } else if (item.Name == "cmsSetStart") //设为起点 { LatLngPoint location = MapHelper.GetLatLngByScreenLocation(_right_mouse_point_cache, _center, _zoom, ClientSize); ((Action)delegate() { GeocodingService gs = new GeocodingService(); JObject point = gs.DeGeocoding(location.Lat + "," + location.Lng); if (point != null) //具体json格式参见api文档 { this.Invoke((Action)delegate() { _current_selected_point = _theRouteStart = new BPoint { Type = PointType.RouteStart, Selected = true, Address = (string)point["result"]["formatted_address"], Location = location }; _bPointTipControl.BPoint = _theRouteStart; _bPointTipControl.Location = new Point(_right_mouse_point_cache.X - _bPointTipControl.Width / 3 + 37, _right_mouse_point_cache.Y - _bPointTipControl.Height - 34); _bPointTipControl.Visible = true; BDirectionBoard.SourcePlace = _theRouteStart.Address; }); } }).BeginInvoke(null, null); } else if (item.Name == "cmsSetEnd") //设为终点 { LatLngPoint location = MapHelper.GetLatLngByScreenLocation(_right_mouse_point_cache, _center, _zoom, ClientSize); ((Action)delegate() { GeocodingService gs = new GeocodingService(); JObject point = gs.DeGeocoding(location.Lat + "," + location.Lng); if (point != null) //具体json格式参见api文档 { this.Invoke((Action)delegate() { _current_selected_point = _theRouteEnd = new BPoint { Type = PointType.RouteEnd, Selected = true, Address = (string)point["result"]["formatted_address"], Location = location }; _bPointTipControl.BPoint = _theRouteEnd; _bPointTipControl.Location = new Point(_right_mouse_point_cache.X - _bPointTipControl.Width / 3 + 37, _right_mouse_point_cache.Y - _bPointTipControl.Height - 34); _bPointTipControl.Visible = true; BDirectionBoard.DestinationPlace = _theRouteEnd.Address; }); } }).BeginInvoke(null, null); } else if (item.Name == "cmsCenter") //居中 { LatLngPoint lp = MapHelper.GetLatLngByScreenLocation(PointToClient(Cursor.Position), _center, _zoom, ClientSize); Center = lp; Locate(false); SyncControlsLocation(); } else if (item.Name == "cmsLarge") //放大 { int z = _zoom + 1; if (z >= 3 && z <= 19) { Zoom = z; Locate(false); SyncControlsLocation(); } } else if (item.Name == "cmsSmall") //缩小 { int z = _zoom - 1; if (z >= 3 && z <= 19) { Zoom = z; Locate(false); SyncControlsLocation(); } } else if (item.Name == "cmsClearDrawings") //清空绘图 { _drawingObjects.Clear(); } else if (item.Name == "cmsClearMarkers") //清空标记 { _markers.Clear(); } else if (item.Name == "cmsRegionSaveAs") //可视区域另存为 { using (SaveFileDialog sf = new SaveFileDialog()) { sf.CheckPathExists = true; sf.Filter = "PNG文件(*.png)|*.png"; sf.DefaultExt = ".png"; if (sf.ShowDialog() == DialogResult.OK) { Bitmap b = new Bitmap(Width, Height); this.DrawToBitmap(b, ClientRectangle); b.Save(sf.FileName); } } } Invalidate(); }
/// <summary> /// 鼠标在地图上按下 /// </summary> /// <param name="e"></param> protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); if (e.Button == System.Windows.Forms.MouseButtons.Left) //左键 { //检查设置鼠标工作方式 if (new Rectangle(Width - 384 + 26 * 8, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //鼠标定位 { _cursor_located = !_cursor_located; Invalidate(); return; } else if (new Rectangle(Width - 384 + 26 * 7, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //绘制多边形 { if (_mouse_type == MouseType.DrawPolygon) { _mouse_type = MouseType.None; _current_cursor_cache = Cursor = Cursors.Arrow; } else { _mouse_type = MouseType.DrawPolygon; _current_cursor_cache = Cursor = Cursors.Cross; //特定光标 _current_drawing = null; _bScreenshotMenu.Visible = false; } Invalidate(); return; } else if (new Rectangle(Width - 384 + 26 * 6, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //绘制直线 { if (_mouse_type == MouseType.DrawLine) { _mouse_type = MouseType.None; _current_cursor_cache = Cursor = Cursors.Arrow; } else { _mouse_type = MouseType.DrawLine; _current_cursor_cache = Cursor = Cursors.Cross; //特定光标 _current_drawing = null; _bScreenshotMenu.Visible = false; } Invalidate(); return; } else if (new Rectangle(Width - 384 + 26 * 5, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //绘制椭圆 { if (_mouse_type == MouseType.DrawCircle) { _mouse_type = MouseType.None; _current_cursor_cache = Cursor = Cursors.Arrow; } else { _mouse_type = MouseType.DrawCircle; _current_cursor_cache = Cursor = Cursors.Cross; //特定光标 _current_drawing = null; _bScreenshotMenu.Visible = false; } Invalidate(); return; } else if (new Rectangle(Width - 384 + 26 * 4, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //绘制矩形 { if (_mouse_type == MouseType.DrawRectange) { _mouse_type = MouseType.None; _current_cursor_cache = Cursor = Cursors.Arrow; } else { _mouse_type = MouseType.DrawRectange; _current_cursor_cache = Cursor = Cursors.Cross; //特定光标 _current_drawing = null; _bScreenshotMenu.Visible = false; } Invalidate(); return; } else if (new Rectangle(Width - 384 + 26 * 3, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //添加标记点 { if (_mouse_type == MouseType.DrawMarker) { _mouse_type = MouseType.None; _current_cursor_cache = Cursor = Cursors.Arrow; } else { _mouse_type = MouseType.DrawMarker; _current_drawing = null; _bScreenshotMenu.Visible = false; } Invalidate(); return; } else if (new Rectangle(Width - 384 + 26 * 2, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //截图 { if (_current_drawing as BScreenShotRectangle == null) { if (_mouse_type == MouseType.DrawScreenshotArea) { _mouse_type = MouseType.None; _current_cursor_cache = Cursor = Cursors.Arrow; } else { _mouse_type = MouseType.DrawScreenshotArea; _current_cursor_cache = Cursor = Cursors.Cross; //特定光标 } Invalidate(); } return; } else if (new Rectangle(Width - 384 + 26 * 1, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //测量距离 { if (_b_distance == null) { if (_mouse_type == MouseType.DrawDistance) { _mouse_type = MouseType.None; _current_cursor_cache = Cursor = Cursors.Arrow; } else { _mouse_type = MouseType.DrawDistance; _current_cursor_cache = Cursor = Cursors.Cross; //特定光标 _current_drawing = null; _bScreenshotMenu.Visible = false; } Invalidate(); } else { _b_distance = null; } return; } else if (new Rectangle(Width - 384, 10, 26, 26).Contains(PointToClient(Cursor.Position))) //矩形区域搜索 { if (_b_bound == null) { if (_mouse_type == MouseType.DrawBound) { _mouse_type = MouseType.None; _current_cursor_cache = Cursor = Cursors.Arrow; } else { _mouse_type = MouseType.DrawBound; _current_cursor_cache = Cursor = Cursors.Cross; //特定光标 _current_drawing = null; _bScreenshotMenu.Visible = false; } Invalidate(); } else { _b_bound = null; _bQuickSearchControl.Visible = false; } return; } if (new Rectangle(10, 10, 90, 25).Contains(e.Location)) //打开城市切换窗体 { if (_bCityControl.Visible) { _bCityControl.Visible = false; } else { _bCityControl.Location = new Point(10, 38); _bCityControl.Visible = true; _bCityControl.CurrentCity = _currentCity; } Invalidate(); return; } else if (new Rectangle(Width - 124, 10, 52, 52).Contains(e.Location)) //打开地图加载模式窗体 { if (_bLoadMapModeControl.Visible) { _bLoadMapModeControl.Visible = false; } else { _bLoadMapModeControl.Location = new Point(Width - 124 + 52 - _bLoadMapModeControl.Width, 10 + 55); _bLoadMapModeControl.LoadMode = _loadmode; _bLoadMapModeControl.Visible = true; } Invalidate(); return; } else if (new Rectangle(Width - 62, 10, 52, 52).Contains(e.Location)) //切换地图模式 { if (_mode == MapMode.Normal) { if (_chkShowRoadNet.Checked) { Mode = MapMode.Sate_RoadNet; } else { Mode = MapMode.Satellite; } _chkShowRoadNet.Location = new Point(Width - 62, 65); _chkShowRoadNet.Visible = true; } else { Mode = MapMode.Normal; _chkShowRoadNet.Visible = false; } return; } if (_mouse_type == MouseType.None) //拖拽地图 { //判断是否拖拽其他物体 地图优先级最低 if (_current_drawing as BScreenShotRectangle != null) //拖拽截图矩形 { if ((_current_drawing as BScreenShotRectangle).Rect.Contains(e.Location)) { _mouse_type = MouseType.DragScreenshotArea; _current_cursor_cache = Cursor = Cursors.SizeAll; _previous_point_cache = e.Location; return; } } if (_theStrangePoint != null && _theStrangePoint.Rect.Contains(e.Location)) //是否点击未知点 { _current_selected_point = _theStrangePoint; Point point = MapHelper.GetScreenLocationByLatLng(_current_selected_point.Location, _center, _zoom, ClientSize); //信息显示控件 _bPointTipControl.BPoint = _current_selected_point; _bPointTipControl.Location = new Point(point.X - _bPointTipControl.Width / 3 + 35, point.Y - _bPointTipControl.Height - _current_selected_point.Rect.Height); _bPointTipControl.Visible = true; return; } if (_theRouteStart !=null && _theRouteStart.Rect.Contains(e.Location)) //是否点击路线起点 { _current_selected_point = _theRouteStart; Point point = MapHelper.GetScreenLocationByLatLng(_current_selected_point.Location, _center, _zoom, ClientSize); //信息显示控件 _bPointTipControl.BPoint = _current_selected_point; _bPointTipControl.Location = new Point(point.X - _bPointTipControl.Width / 3 + 35, point.Y - _bPointTipControl.Height - _current_selected_point.Rect.Height); _bPointTipControl.Visible = true; return; } if (_theRouteEnd != null && _theRouteEnd.Rect.Contains(e.Location)) //是否点击路线终点 { _current_selected_point = _theRouteEnd; Point point = MapHelper.GetScreenLocationByLatLng(_current_selected_point.Location, _center, _zoom, ClientSize); //信息显示控件 _bPointTipControl.BPoint = _current_selected_point; _bPointTipControl.Location = new Point(point.X - _bPointTipControl.Width / 3 + 35, point.Y - _bPointTipControl.Height - _current_selected_point.Rect.Height); _bPointTipControl.Visible = true; return; } foreach (KeyValuePair<string,BPOI> p in _pois) //是否点击POI点 { if (p.Value.Rect.Contains(e.Location)) { _current_selected_poi = p.Value; //显示信息控件 p.Value.Selected = true; Point point = MapHelper.GetScreenLocationByLatLng(p.Value.Location, _center, _zoom, ClientSize); _bPOITipControl.POI = _current_selected_poi; _bPOITipControl.Location = new Point(point.X - _bPOITipControl.Width / 3 + 35, point.Y - _bPOITipControl.Height - _current_selected_poi.Rect.Height - 5); _bPOITipControl.Visible = true; foreach (KeyValuePair<string, BPOI> pp in _pois) { if (pp.Value != p.Value) { pp.Value.Selected = false; } } Invalidate(); //通知BPlacesBoard 同步选择 if (BPlacesBoard != null) { BPlacesBoard.SelectPlace(p.Value); } return; } } foreach (KeyValuePair<string, BMarker> p in _markers) //是否点击标记点 { if (p.Value.Rect.Contains(e.Location)) { _current_selected_marker = p.Value; //显示标记信息控件 Point point = MapHelper.GetScreenLocationByLatLng(p.Value.Location, _center, _zoom, ClientSize); _bMarkerTipControl.Deleted = false; _bMarkerTipControl.Edited = false; _bMarkerTipControl.Marker = _current_selected_marker; _bMarkerTipControl.Location = new Point(point.X - _bMarkerTipControl.Width / 3 + 37, point.Y - _bMarkerTipControl.Height - p.Value.Rect.Height); _bMarkerTipControl.Visible = true; return; } } _bCityControl.Visible = false; _bLoadMapModeControl.Visible = false; _mouse_type = MouseType.DragMap; _current_cursor_cache = Cursor = Cursors.SizeAll; _previous_point_cache = e.Location; } else if (_mouse_type == MouseType.DrawCircle) //绘制椭圆 { LatLngPoint theCenter = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize); _current_drawing = new BCircle { Center = theCenter, RightBottom = theCenter }; } else if (_mouse_type == MouseType.DrawRectange) //绘制矩形 { LatLngPoint leftTop = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize); _current_drawing = new BRectangle { LeftTop = leftTop, RightBottom = leftTop }; } else if (_mouse_type == MouseType.DrawLine) //绘制直线 { LatLngPoint p = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize); if (_current_drawing == null) { _current_drawing = new BLine { Points = new List<LatLngPoint> { p, p } }; } (_current_drawing as BLine).Points.Add(p); } else if (_mouse_type == MouseType.DrawPolygon) //绘制多边形 { LatLngPoint p = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize); if (_current_drawing == null) { _current_drawing = new BPolygon { Points = new List<LatLngPoint> { p, p } }; } (_current_drawing as BPolygon).Points.Add(p); } else if (_mouse_type == MouseType.DrawScreenshotArea) //绘制截图区域 { _current_drawing = new BScreenShotRectangle { LeftTop = e.Location, Width = 0, Height = 0 }; } else if (_mouse_type == MouseType.DrawBound) //矩形搜索开始 { LatLngPoint leftTop = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize); _b_bound = new BBound { LeftTop = leftTop, RightBottom = leftTop }; } else if (_mouse_type == MouseType.DrawDistance) //距离测量 { LatLngPoint p = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize); if (_b_distance == null) { _b_distance = new BDistance { Points = new List<LatLngPoint> { p, p } }; } _b_distance.Points.Add(p); } else if (_mouse_type == MouseType.DrawMarker) //添加标记 { LatLngPoint p = MapHelper.GetLatLngByScreenLocation(e.Location, _center, _zoom, ClientSize); ((Action)delegate() { GeocodingService gs = new GeocodingService(); JObject place = gs.DeGeocoding(p.Lat + "," + p.Lng); if (place != null) { this.Invoke((Action)delegate() { BMarker marker = new BMarker { Index = _markers.Count, Location = p, Name = (string)place["result"]["formatted_address"], Remarks = "我的备注", Selected = false, Address = (string)place["result"]["formatted_address"] }; _markers.Add(marker.Index.ToString(), marker); _bMarkerEditorControl.Saved = false; _bMarkerEditorControl.Marker = marker; _bMarkerEditorControl.Location = new Point(e.Location.X - _bMarkerEditorControl.Width / 3 + 37, e.Location.Y - _bMarkerEditorControl.Height - 22); _bMarkerEditorControl.Visible = true; _current_selected_marker = marker; }); } }).BeginInvoke(null, null); } } if (e.Button == System.Windows.Forms.MouseButtons.Right) //右键弹菜单 { _right_mouse_point_cache = e.Location; cm_popup.Show(PointToScreen(e.Location)); } }
/// <summary> /// 定位 /// </summary> /// <param name="mylocation">为true表示定位自己 否则定位当前地图中的城市</param> private void Locate(bool mylocation) { //定位位置 ((Action)(delegate() { if (mylocation) //定位自己 { IPService ips = new IPService(); JObject _location = ips.LocationByIP(); if (_location != null && _location["content"] != null) { _currentCity = (string)(_location["content"]["address_detail"]["city"]); //返回JSON结构请参见百度API文档 _center = new LatLngPoint(double.Parse((string)_location["content"]["point"]["x"]), double.Parse((string)_location["content"]["point"]["y"])); } } else //定位地图中心点 { GeocodingService gs = new GeocodingService(); JObject _location = gs.DeGeocoding(_center.Lat + "," + _center.Lng); if (_location != null) { if (_location["result"] != null && _location["result"]["addressComponent"] != null) { if (_zoom <= 8) //定位到国家 { _currentCity = (string)(_location["result"]["addressComponent"]["country"]); //返回JSON结构请参见百度API文档 } else if (_zoom <= 10) //定位到省份 { _currentCity = (string)(_location["result"]["addressComponent"]["province"]); //返回JSON结构请参见百度API文档 } else if (_zoom <= 18) //定位到城市 { if (_location["result"]["addressComponent"]["city"] != null) _currentCity = (string)(_location["result"]["addressComponent"]["city"]); //返回JSON结构请参见百度API文档 } //else //定位到县区 //{ // if (_location["result"]["addressComponent"]["district"] != null) // _currentCity = (string)(_location["result"]["addressComponent"]["district"]); //返回JSON结构请参见百度API文档 //} } } } this.Invoke((Action)delegate() { Invalidate(); if (BPlaceBox != null) //关联的位置输入控件 { BPlaceBox.CurrentCity = _currentCity; } if (BPlacesBoard != null) //关联的位置列表控件 { BPlacesBoard.CurrentCity = _currentCity; } if (BDirectionBoard != null) //关联的导航控件 { BDirectionBoard.CurrentCity = _currentCity; } _bMarkerTipControl.CurrentCity = _currentCity; _bPOITipControl.CurrentCity = _currentCity; _bPointTipControl.CurrentCity = _currentCity; }); })).BeginInvoke(null, null); }