/// <summary> /// Creates a new map rectangle structure with the specified location and size. /// </summary> /// <param name="location">The rectangle location.</param> /// <param name="size">The rectangle size.</param> public MapRectangle(MapPoint location, MapSize size) { this.Left = location.X; this.Top = location.Y; this.Right = location.X + size.Width; this.Bottom = location.Y - size.Height; }
/// <summary> /// Updates the map grid. /// </summary> private void OnUpdateGrid() { MapSize majorGridSize = new MapSize(1.0, 1.0); MapSize minorGridSize = new MapSize(1.0, 1.0); Point majorGridBegin; Point majorGridEnd; Point minorGridBegin; Point minorGridEnd; // Compute the grid size. majorGridSize = new MapSize( Math.Pow(10.0, Math.Floor(Math.Log10(this.mapSize.Width))), Math.Pow(10.0, Math.Floor(Math.Log10(this.mapSize.Height))) ); // Adjust the horizontal grid. if (majorGridSize.Width > 10.0) { // Select the first value than results in a displayed width greater than the minimum width. for (int index = 0; index < MapControl.majorGridValues.Length; index++) { if (majorGridValues[index] * this.mapScale.Width >= MapControl.majorGridMinimumSize.Width) { majorGridSize.Width = MapControl.majorGridValues[index]; minorGridSize.Width = MapControl.minorGridValues[index]; break; } } // Limit the grid size to the highest value. if (majorGridSize.Width > MapControl.majorGridValues[MapControl.majorGridValues.Length - 1]) { majorGridSize.Width = MapControl.majorGridValues[MapControl.majorGridValues.Length - 1]; minorGridSize.Width = MapControl.minorGridValues[MapControl.majorGridValues.Length - 1]; } } else { // Select the first factor that results in a displayed width greater than the minimum width. for (int index = 0; index < MapControl.majorGridFactor.Length; index++) { if (majorGridSize.Width * MapControl.majorGridFactor[index] >= MapControl.majorGridMinimumSize.Width) { majorGridSize.Width *= MapControl.majorGridFactor[index]; minorGridSize.Width = majorGridSize.Width * minorGridFactor[index]; break; } } } // Adjust the vertical grid. if (majorGridSize.Height > 10.0) { // Select the first value than results in a displayed height greater than the minimum height. for (int index = 0; index < MapControl.majorGridValues.Length; index++) { if ( MapControl.majorGridValues[index] * this.mapScale.Height >= MapControl.majorGridMinimumSize.Height) { majorGridSize.Height = MapControl.majorGridValues[index]; minorGridSize.Height = MapControl.minorGridValues[index]; break; } } // Limit the grid size to the highest value. if (majorGridSize.Height > MapControl.majorGridValues[MapControl.majorGridValues.Length - 1]) { majorGridSize.Height = MapControl.majorGridValues[MapControl.majorGridValues.Length - 1]; minorGridSize.Height = MapControl.minorGridValues[MapControl.majorGridValues.Length - 1]; } } else { // Select the first factor that results in a displayed height greater than the minimum height. for (int index = 0; index < MapControl.majorGridFactor.Length; index++) { if (majorGridSize.Height * MapControl.majorGridFactor[index] >= MapControl.majorGridMinimumSize.Height) { majorGridSize.Height *= MapControl.majorGridFactor[index]; minorGridSize.Height = majorGridSize.Height * MapControl.minorGridFactor[index]; break; } } } // Compute the major grid begin and end. majorGridBegin = new Point( this.mapBounds.Left % majorGridSize.Width == 0 ? (int)Math.Ceiling(this.mapBounds.Left / majorGridSize.Width) + 1 : (int)Math.Ceiling(this.mapBounds.Left / majorGridSize.Width), this.mapBounds.Top % majorGridSize.Height == 0 ? (int)Math.Floor(this.mapBounds.Top / majorGridSize.Height) - 1 : (int)Math.Floor(this.mapBounds.Top / majorGridSize.Height) ); majorGridEnd = new Point( this.mapBounds.Right % majorGridSize.Width == 0 ? (int)Math.Floor(this.mapBounds.Right / majorGridSize.Width) - 1 : (int)Math.Floor(this.mapBounds.Right / majorGridSize.Width), this.mapBounds.Bottom % majorGridSize.Height == 0 ? (int)Math.Ceiling(this.mapBounds.Bottom / majorGridSize.Height) + 1 : (int)Math.Ceiling(this.mapBounds.Bottom / majorGridSize.Height) ); // Compute the minor grid begin and end. minorGridBegin = new Point( this.mapBounds.Left % minorGridSize.Width == 0 ? (int)Math.Ceiling(this.mapBounds.Left / minorGridSize.Width) + 1 : (int)Math.Ceiling(this.mapBounds.Left / minorGridSize.Width), this.mapBounds.Top % minorGridSize.Height == 0 ? (int)Math.Floor(this.mapBounds.Top / minorGridSize.Height) - 1 : (int)Math.Floor(this.mapBounds.Top / minorGridSize.Height) ); minorGridEnd = new Point( this.mapBounds.Right % minorGridSize.Width == 0 ? (int)Math.Floor(this.mapBounds.Right / minorGridSize.Width) - 1 : (int)Math.Floor(this.mapBounds.Right / minorGridSize.Width), this.mapBounds.Bottom % minorGridSize.Height == 0 ? (int)Math.Ceiling(this.mapBounds.Bottom / minorGridSize.Height) + 1 : (int)Math.Ceiling(this.mapBounds.Bottom / minorGridSize.Height) ); // Compute the horizontal major grid. this.majorGridHorizontalCoordinate = new double[majorGridEnd.X - majorGridBegin.X + 1]; this.majorGridHorizontalPoint = new float[this.majorGridHorizontalCoordinate.Length]; for (int index = 0; index < this.majorGridHorizontalPoint.Length; index++) { this.majorGridHorizontalCoordinate[index] = (majorGridBegin.X + index) * majorGridSize.Width; this.majorGridHorizontalPoint[index] = this.ConvertLongitudeF(this.majorGridHorizontalCoordinate[index]); } // Compute the vertical major grid. this.majorGridVerticalCoordinate = new double[majorGridBegin.Y - majorGridEnd.Y + 1]; this.majorGridVerticalPoint = new float[this.majorGridVerticalCoordinate.Length]; for (int index = 0; index < this.majorGridVerticalPoint.Length; index++) { this.majorGridVerticalCoordinate[index] = (majorGridEnd.Y + index) * majorGridSize.Height; this.majorGridVerticalPoint[index] = this.ConvertLatitudeF(this.majorGridVerticalCoordinate[index]); } }
/// <summary> /// Saves the current map as an image to the specified file. /// </summary> /// <param name="fileName">The file name.</param> /// <param name="properties">The export properties.</param> /// <param name="format">The image format.</param> public void SaveMap(string fileName, ExportProperties properties, ImageFormat format) { // Create a new bitmap. Bitmap bitmap = new Bitmap(properties.Size.Width, properties.Size.Height); // Compute the map scale. MapScale mapScale = new MapScale( properties.Size.Width / this.mapBounds.Width, properties.Size.Height / this.mapBounds.Height ); // Compute the grid. MapSize majorGridSize = new MapSize(1.0, 1.0); MapSize minorGridSize = new MapSize(1.0, 1.0); Point majorGridBegin; Point majorGridEnd; Point minorGridBegin; Point minorGridEnd; // Compute the grid size. majorGridSize = new MapSize( Math.Pow(10.0, Math.Floor(Math.Log10(this.mapSize.Width))), Math.Pow(10.0, Math.Floor(Math.Log10(this.mapSize.Height))) ); // Adjust the horizontal grid. if (majorGridSize.Width > 10.0) { // Select the first value than results in a displayed width greater than the minimum width. for (int index = 0; index < MapControl.majorGridValues.Length; index++) { if (majorGridValues[index] * mapScale.Width >= MapControl.majorGridMinimumSize.Width) { majorGridSize.Width = MapControl.majorGridValues[index]; minorGridSize.Width = MapControl.minorGridValues[index]; break; } } // Limit the grid size to the highest value. if (majorGridSize.Width > MapControl.majorGridValues[MapControl.majorGridValues.Length - 1]) { majorGridSize.Width = MapControl.majorGridValues[MapControl.majorGridValues.Length - 1]; minorGridSize.Width = MapControl.minorGridValues[MapControl.majorGridValues.Length - 1]; } } else { // Select the first factor that results in a displayed width greater than the minimum width. for (int index = 0; index < MapControl.majorGridFactor.Length; index++) { if (majorGridSize.Width * MapControl.majorGridFactor[index] >= MapControl.majorGridMinimumSize.Width) { majorGridSize.Width *= MapControl.majorGridFactor[index]; minorGridSize.Width = majorGridSize.Width * minorGridFactor[index]; break; } } } // Adjust the vertical grid. if (majorGridSize.Height > 10.0) { // Select the first value than results in a displayed height greater than the minimum height. for (int index = 0; index < MapControl.majorGridValues.Length; index++) { if (MapControl.majorGridValues[index] * mapScale.Height >= MapControl.majorGridMinimumSize.Height) { majorGridSize.Height = MapControl.majorGridValues[index]; minorGridSize.Height = MapControl.minorGridValues[index]; break; } } // Limit the grid size to the highest value. if (majorGridSize.Height > MapControl.majorGridValues[MapControl.majorGridValues.Length - 1]) { majorGridSize.Height = MapControl.majorGridValues[MapControl.majorGridValues.Length - 1]; minorGridSize.Height = MapControl.minorGridValues[MapControl.majorGridValues.Length - 1]; } } else { // Select the first factor that results in a displayed height greater than the minimum height. for (int index = 0; index < MapControl.majorGridFactor.Length; index++) { if (majorGridSize.Height * MapControl.majorGridFactor[index] >= MapControl.majorGridMinimumSize.Height) { majorGridSize.Height *= MapControl.majorGridFactor[index]; minorGridSize.Height = majorGridSize.Height * MapControl.minorGridFactor[index]; break; } } } // Compute the major grid begin and end. majorGridBegin = new Point( this.mapBounds.Left % majorGridSize.Width == 0 ? (int)Math.Ceiling(this.mapBounds.Left / majorGridSize.Width) + 1 : (int)Math.Ceiling(this.mapBounds.Left / majorGridSize.Width), this.mapBounds.Top % majorGridSize.Height == 0 ? (int)Math.Floor(this.mapBounds.Top / majorGridSize.Height) - 1 : (int)Math.Floor(this.mapBounds.Top / majorGridSize.Height) ); majorGridEnd = new Point( this.mapBounds.Right % majorGridSize.Width == 0 ? (int)Math.Floor(this.mapBounds.Right / majorGridSize.Width) - 1 : (int)Math.Floor(this.mapBounds.Right / majorGridSize.Width), this.mapBounds.Bottom % majorGridSize.Height == 0 ? (int)Math.Ceiling(this.mapBounds.Bottom / majorGridSize.Height) + 1 : (int)Math.Ceiling(this.mapBounds.Bottom / majorGridSize.Height) ); // Compute the minor grid begin and end. minorGridBegin = new Point( this.mapBounds.Left % minorGridSize.Width == 0 ? (int)Math.Ceiling(this.mapBounds.Left / minorGridSize.Width) + 1 : (int)Math.Ceiling(this.mapBounds.Left / minorGridSize.Width), this.mapBounds.Top % minorGridSize.Height == 0 ? (int)Math.Floor(this.mapBounds.Top / minorGridSize.Height) - 1 : (int)Math.Floor(this.mapBounds.Top / minorGridSize.Height) ); minorGridEnd = new Point( this.mapBounds.Right % minorGridSize.Width == 0 ? (int)Math.Floor(this.mapBounds.Right / minorGridSize.Width) - 1 : (int)Math.Floor(this.mapBounds.Right / minorGridSize.Width), this.mapBounds.Bottom % minorGridSize.Height == 0 ? (int)Math.Ceiling(this.mapBounds.Bottom / minorGridSize.Height) + 1 : (int)Math.Ceiling(this.mapBounds.Bottom / minorGridSize.Height) ); // Compute the horizontal major grid. double[] majorGridHorizontalCoordinate = new double[majorGridEnd.X - majorGridBegin.X + 1]; float[] majorGridHorizontalPoint = new float[majorGridHorizontalCoordinate.Length]; for (int index = 0; index < majorGridHorizontalPoint.Length; index++) { majorGridHorizontalCoordinate[index] = (majorGridBegin.X + index) * majorGridSize.Width; majorGridHorizontalPoint[index] = (float)((majorGridHorizontalCoordinate[index] - this.mapBounds.Left) * mapScale.Width); } // Compute the vertical major grid. double[] majorGridVerticalCoordinate = new double[majorGridBegin.Y - majorGridEnd.Y + 1]; float[] majorGridVerticalPoint = new float[majorGridVerticalCoordinate.Length]; for (int index = 0; index < majorGridVerticalPoint.Length; index++) { majorGridVerticalCoordinate[index] = (majorGridEnd.Y + index) * majorGridSize.Height; majorGridVerticalPoint[index] = (float)((this.mapBounds.Top - majorGridVerticalCoordinate[index]) * mapScale.Height); } // Draw the bitmap. using (Graphics graphics = Graphics.FromImage(bitmap)) { // Set the smooting mode. graphics.SmoothingMode = properties.Smoothing; using (SolidBrush brush = new SolidBrush(properties.BackgroundColor)) { using (Pen pen = new Pen(properties.RegionNormalBorderColor)) { // Fill the background. graphics.FillRectangle(brush, 0, 0, properties.Size.Width, properties.Size.Height); // Change the brush color. brush.Color = properties.RegionNormalBackgroundColor; // Get an exclusive reader lock to the regions list. this.regions.Lock(); try { // Draw the map regions. foreach (MapRegion region in this.regions) { // Draw the region. region.Draw(graphics, this.mapBounds, mapScale, brush, properties.Borders ? pen : null); } } finally { this.regions.Unlock(); } // Draw the major grid. if (properties.MajorGrid) { // Set the pen color. pen.Color = properties.MajorGridColor; // Set the brush color. brush.Color = Color.Black; // Draw the horizontal grid. if (null != majorGridHorizontalPoint) { for (int index = 0; index < majorGridHorizontalPoint.Length; index++) { // Draw the grid line. graphics.DrawLine(pen, majorGridHorizontalPoint[index], 0, majorGridHorizontalPoint[index], properties.Size.Height - 1); // Draw the coordinates. graphics.DrawString( majorGridHorizontalCoordinate[index].LongitudeToString(), this.fontGrid, brush, new Point((int)majorGridHorizontalPoint[index], 0)); } } // Draw the vertical grid. if (null != majorGridVerticalPoint) { for (int index = 0; index < majorGridVerticalPoint.Length; index++) { // Draw the grid line. graphics.DrawLine(pen, 0, majorGridVerticalPoint[index], properties.Size.Width - 1, majorGridVerticalPoint[index]); // Draw the coordinates. graphics.DrawString( majorGridVerticalCoordinate[index].LatitudeToString(), this.fontGrid, brush, new Point(0, (int)majorGridVerticalPoint[index])); } } } // If the show markers flag is set. if (properties.Markers) { // Change the pen and brush colors. pen.Color = properties.MarkerNormalBorderColor; brush.Color = properties.MarkerNormalBackgroundColor; // Try lockLock the markers collection. if (this.markers.TryLock()) { try { // Draw the normal markers. foreach (MapMarker marker in this.markers) { if (!marker.Emphasized) { marker.Draw(graphics, this.mapBounds, mapScale, brush, pen); } } // Change the pen and brush colors. pen.Color = properties.MarkerEmphasisBorderColor; brush.Color = properties.MarkerEmphasisBackgroundColor; // Draw the emphasized markers. foreach (MapMarker marker in this.markers) { if (marker.Emphasized) { marker.Draw(graphics, this.mapBounds, mapScale, brush, pen); } } } finally { this.markers.Unlock(); } } } } } } // Check the directory exists. if (Directory.EnsureFileDirectoryExists(fileName)) { // Save the bitmap to file. bitmap.Save(fileName, format); } }
/// <summary> /// An event handler called when the map bounds have changed. /// </summary> /// <param name="mapBounds">The map bounds.</param> private void OnMapBoundsChanged(MapRectangle mapBounds) { // Compute the map size to use the default values in case the width and height are zero. this.mapSize = new MapSize( mapBounds.Width != 0 ? mapBounds.Width : MapControl.mapBoundsDefault.Width, mapBounds.Height != 0 ? mapBounds.Height : MapControl.mapBoundsDefault.Height ); // Compute the map bounds. this.mapBounds = new MapRectangle( new MapPoint(mapBounds.Left, mapBounds.Top), this.mapSize ); // Recompute the map scale and bitmap size. this.OnMapSizeChanged(); // Refresh the current map. this.OnRefreshMap(); }