Skip to content

Commit

Permalink
Some fixes, added new events and properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Krutov committed Sep 24, 2020
1 parent ccba9ca commit cf953c8
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 37 deletions.
22 changes: 2 additions & 20 deletions DemoApp/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private ICollection<GeoPoint> ReadPointsFromResource(string resourceName)
private void UpdateWindowTitle()
{
GeoPoint g = mapControl.Mouse;
this.Text = $"Longitude = {DegreeToString(g.Longitude, "W", "E")} / Latitude = {DegreeToString(g.Latitude, "S", "N")} / Zoom = {mapControl.ZoomLevel}";
this.Text = $"Mouse = {g} / Zoom = {mapControl.ZoomLevel} / Bounding Box = TL:{mapControl.TopLeft}, TR:{mapControl.TopRight}, BR:{mapControl.BottomRight}, BL:{mapControl.BottomLeft}";
}

private void mapControl_MouseMove(object sender, MouseEventArgs e)
Expand Down Expand Up @@ -184,26 +184,8 @@ private void mapControl_DoubleClick(object sender, EventArgs e)
{
var coord = mapControl.Mouse;
StringBuilder sb = new StringBuilder();
sb.AppendLine($"Latitude: {DegreeToString(coord.Latitude, "S", "N")}");
sb.AppendLine($"Longitude: {DegreeToString(coord.Longitude, "W", "E")}");
sb.AppendLine($"Location: {coord}");
MessageBox.Show(sb.ToString(), "Info");
}

private string DegreeToString(double coordinate, string negativeSym, string positiveSym)
{
string sym = coordinate < 0d ? negativeSym : positiveSym;
coordinate = Math.Abs(coordinate);
double d = Math.Floor(coordinate);
coordinate -= d;
coordinate *= 60;
double m = Math.Floor(coordinate);
coordinate -= m;
coordinate *= 60;
double s = coordinate;
string dd = d.ToString();
string mm = m.ToString().PadLeft(2, '0');
string ss = s.ToString("00.00", CultureInfo.InvariantCulture);
return $"{dd}° {mm}' {ss}\" {sym}";
}
}
}
27 changes: 26 additions & 1 deletion MapControl/GeoPoint.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace System.Windows.Forms
using System.Globalization;

namespace System.Windows.Forms
{
/// <summary>
/// Represents point on the Earth surface with geographical coordinates
Expand All @@ -25,5 +27,28 @@ public GeoPoint(float longitude, float latitude)
Longitude = longitude;
Latitude = latitude;
}

/// <inheritdoc/>
public override string ToString()
{
return $"{DegreeToString(Longitude, "W", "E")}, {DegreeToString(Latitude, "S", "N")}";
}

private static string DegreeToString(double coordinate, string negativeSym, string positiveSym)
{
string sym = coordinate < 0d ? negativeSym : positiveSym;
coordinate = Math.Abs(coordinate);
double d = Math.Floor(coordinate);
coordinate -= d;
coordinate *= 60;
double m = Math.Floor(coordinate);
coordinate -= m;
coordinate *= 60;
double s = coordinate;
string dd = d.ToString();
string mm = m.ToString().PadLeft(2, '0');
string ss = s.ToString("00.00", CultureInfo.InvariantCulture);
return $"{dd}° {mm}' {ss}\" {sym}";
}
}
}
127 changes: 112 additions & 15 deletions MapControl/MapControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -93,7 +94,8 @@ public int ZoomLevel
if (value < 0 || value > 19)
throw new ArgumentException($"{value} is an incorrect value for {nameof(ZoomLevel)} property. Value should be in range from 0 to 19.");

SetZoomLevel(value, new Point(Width / 2, Height / 2));
SetZoomLevel(value, new Point(Width / 2, Height / 2));
CenterChanged?.Invoke(this, EventArgs.Empty);
}
}

Expand Down Expand Up @@ -205,7 +207,8 @@ public ITileServer TileServer
ZoomLevel = TileServer.MinZoomLevel;
}

Invalidate();
Invalidate();
TileServerChanged?.Invoke(this, EventArgs.Empty);
}
}

Expand All @@ -218,8 +221,8 @@ public GeoPoint Center
{
get
{
float x = NormalizeTileNumber(-(_Offset.X - Width / 2) / TILE_SIZE);
float y = -(_Offset.Y - Height / 2) / TILE_SIZE;
float x = NormalizeTileNumber(-(float)(_Offset.X - Width / 2) / TILE_SIZE);
float y = -(float)(_Offset.Y - Height / 2) / TILE_SIZE;
return TileToWorldPos(x, y);
}
set
Expand All @@ -228,11 +231,11 @@ public GeoPoint Center
_Offset.X = -(int)(center.X * TILE_SIZE) + Width / 2;
_Offset.Y = -(int)(center.Y * TILE_SIZE) + Height / 2;
_Offset.X = (int)(_Offset.X % FullMapSizeInPixels);
Invalidate();
Invalidate();
CenterChanged?.Invoke(this, EventArgs.Empty);
}
}


/// <summary>
/// Gets geographical coordinates of the current position of mouse.
/// </summary>
Expand All @@ -248,6 +251,66 @@ public GeoPoint Mouse
}
}

/// <summary>
/// Gets geographical coordinates of the top left point of the map
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public GeoPoint TopLeft
{
get
{
float x = NormalizeTileNumber(-(float)(_Offset.X) / TILE_SIZE);
float y = -(float)(_Offset.Y) / TILE_SIZE;
return TileToWorldPos(x, y);
}
}

/// <summary>
/// Gets geographical coordinates of the top right point of the map
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public GeoPoint TopRight
{
get
{
float x = NormalizeTileNumber(-(float)(_Offset.X - Width) / TILE_SIZE);
float y = -(float)(_Offset.Y) / TILE_SIZE;
return TileToWorldPos(x, y);
}
}

/// <summary>
/// Gets geographical coordinates of the bottom left point of the map
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public GeoPoint BottomLeft
{
get
{
float x = NormalizeTileNumber(-(float)(_Offset.X) / TILE_SIZE);
float y = -(float)(_Offset.Y - Height) / TILE_SIZE;
return TileToWorldPos(x, y);
}
}

/// <summary>
/// Gets geographical coordinates of the bottom right point of the map
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public GeoPoint BottomRight
{
get
{
float x = NormalizeTileNumber(-(float)(_Offset.X - Width) / TILE_SIZE);
float y = -(float)(_Offset.Y - Height) / TILE_SIZE;
return TileToWorldPos(x, y);
}
}

/// <summary>
/// Gets collection of markers to be displayed on the map.
/// </summary>
Expand Down Expand Up @@ -332,6 +395,12 @@ public override Color ForeColor
}
}

/// <summary>
/// Image attributes to be applied to a tile image when drawing.
/// </summary>
[Description("Image attributes to be applied to a tile image when drawing."), Category("Appearance")]
public ImageAttributes TileImageAttributes { get; set; } = null;

/// <summary>
/// Raised when marker is drawn on the map.
/// </summary>
Expand All @@ -346,6 +415,21 @@ public override Color ForeColor
/// Raised when polygon is drawn on the map.
/// </summary>
public event EventHandler<DrawPolygonEventArgs> DrawPolygon;

/// <summary>
/// Raised when <see cref="Center"/> property value is changed.
/// </summary>
public event EventHandler<EventArgs> CenterChanged;

/// <summary>
/// Raised when <see cref="Mouse"/> property value is changed.
/// </summary>
public event EventHandler<EventArgs> MouseChanged;

/// <summary>
/// Raised when <see cref="TileServer"/> property value is changed.
/// </summary>
public event EventHandler<EventArgs> TileServerChanged;

/// <summary>
/// Creates new <see cref="MapControl"/> control.
Expand Down Expand Up @@ -439,7 +523,8 @@ protected override void OnSizeChanged(EventArgs e)
_LinkLabel.Top = Height - _LinkLabel.Height;

AdjustMapBounds();
Invalidate();
Invalidate();
CenterChanged?.Invoke(this, EventArgs.Empty);
}

/// <summary>
Expand Down Expand Up @@ -487,12 +572,14 @@ protected override void OnMouseMove(MouseEventArgs e)
_Offset.Y = Height;

AdjustMapBounds();

Invalidate();
Invalidate();
CenterChanged?.Invoke(this, EventArgs.Empty);
}

_LastMouse.X = e.X;
_LastMouse.Y = e.Y;

MouseChanged?.Invoke(this, EventArgs.Empty);

base.OnMouseMove(e);
}
Expand All @@ -513,8 +600,8 @@ protected override void OnMouseWheel(MouseEventArgs e)
SetZoomLevel(z, new Point(e.X, e.Y));

AdjustMapBounds();

base.OnMouseWheel(e);
base.OnMouseWheel(e);
CenterChanged?.Invoke(this, EventArgs.Empty);
}

/// <summary>
Expand Down Expand Up @@ -744,7 +831,6 @@ private void DrawPolygons(Graphics gr)
using (GraphicsPath gp = new GraphicsPath())
{
gp.StartFigure();

for (int i = 0; i < polygon.Count; i++)
{
GeoPoint g = polygon.ElementAt(i);
Expand All @@ -754,9 +840,12 @@ private void DrawPolygons(Graphics gr)
p = p0.Nearest(p, new PointF(p.X - FullMapSizeInPixels, p.Y), new PointF(p.X + FullMapSizeInPixels, p.Y));
gp.AddLine(p0, p);
}

p0 = p;
}

gp.CloseFigure();

var eventArgs = new DrawPolygonEventArgs()
{
Graphics = gr,
Expand Down Expand Up @@ -812,8 +901,15 @@ private void DrawTilePart(Graphics gr, int x, int y, int xRemainder, int yRemain
Rectangle srcRect = new Rectangle(TILE_SIZE / frac * xRemainder, TILE_SIZE / frac * yRemainder, TILE_SIZE / frac, TILE_SIZE / frac);

// Destination rectangle
Rectangle destRect = new Rectangle(p.X, p.Y, TILE_SIZE, TILE_SIZE);
gr.DrawImage(image, destRect, srcRect, GraphicsUnit.Pixel);
Rectangle destRect = new Rectangle(p.X - frac, p.Y - frac, TILE_SIZE + 2 * frac, TILE_SIZE + 2 * frac);

var state = gr.Save();
gr.SmoothingMode = SmoothingMode.HighSpeed;
gr.InterpolationMode = InterpolationMode.NearestNeighbor;
gr.PixelOffsetMode = PixelOffsetMode.HighSpeed;
gr.CompositingQuality = CompositingQuality.HighSpeed;
gr.DrawImage(image, destRect, srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height, GraphicsUnit.Pixel, TileImageAttributes);
gr.Restore(state);
}

/// <summary>
Expand All @@ -828,7 +924,8 @@ private void DrawTile(Graphics gr, int x, int y, Image image)
Point p = new Point();
p.X = _Offset.X + x * TILE_SIZE;
p.Y = _Offset.Y + y * TILE_SIZE;
gr.DrawImageUnscaled(image, p);

gr.DrawImage(image, new Rectangle(p, new Drawing.Size(TILE_SIZE, TILE_SIZE)), 0, 0, TILE_SIZE, TILE_SIZE, GraphicsUnit.Pixel, TileImageAttributes);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion MapControl/MapControl.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net451</TargetFrameworks>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<Product>System.Windows.Forms.MapControl</Product>
<Description>Map control for WindowsForms</Description>
<Copyright>© Alexander Krutov 2020</Copyright>
Expand Down
43 changes: 43 additions & 0 deletions MapControl/System.Windows.Forms.MapControl.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cf953c8

Please sign in to comment.