I am using GMAP.NET in c#. I am able to display the map on the form, now i am trying to draw a CIRCLE mouse by clicking on a certian point, keeping the left mouse button an
If you want to use the typical GDI features associated with the drawing class, you can simply inherit the GMapMarker class. This allows you to draw simple shapes, like circles, and create custom properties (for instance, one that will calculate the radius in miles of the shape):
public class GMapPoint : GMap.NET.WindowsForms.GMapMarker
{
private PointLatLng point_;
private float size_;
public PointLatLng Point
{
get
{
return point_;
}
set
{
point_ = value;
}
}
public GMapPoint(PointLatLng p, int size)
: base(p)
{
point_ = p;
size_ = size;
}
public override void OnRender(Graphics g)
{
g.FillRectangle(Brushes.Black, LocalPosition.X, LocalPosition.Y, size_, size_);
//OR
g.DrawEllipse(Pens.Black, LocalPosition.X, LocalPosition.Y, size_, size_);
//OR whatever you need
}
}
To draw points on the map:
GMapOverlay points_ = new GMapOverlay("pointCollection");
points_.Markers.Add(new GMapPoint(new PointLatLng(35.06, -106.36), 10));
gMapControl1.Overlays.Add(points_);
(And because I had some questions about it) Since we are inhereting from the markers class, we can still take advantage of the tooltiptext capability:
GMapPoint pnt = new GMapPoint(new PointLatLng(35.06, -106.36), 10);
pnt.Size = new Size(10,10);
pnt.ToolTipText = "Text Here";
pnt.ToolTipMode = MarkerTooltipMode.Always;
points_.AddMarker(pnt);
GMapOverlay markers = new GMapOverlay("markers");
private void CreateCircle(Double lat, Double lon, double radius, int segments)
{
markers.Polygons.Clear();
PointLatLng point = new PointLatLng(lat, lon);
List<PointLatLng> gpollist = new List<PointLatLng>();
for (int i = 0; i < segments; i++)
gpollist.Add(FindPointAtDistanceFrom(point, i, radius / 1000));
List<PointLatLng> gpollistR = new List<PointLatLng>();
List<PointLatLng> gpollistL = new List<PointLatLng>();
foreach (var gp in gpollist)
{
if (gp.Lng > lon)
{
gpollistR.Add(gp);
}
else
{
gpollistL.Add(gp);
}
}
gpollist.Clear();
List<PointLatLng> gpollistRT = new List<PointLatLng>();
List<PointLatLng> gpollistRB = new List<PointLatLng>();
foreach (var gp in gpollistR)
{
if (gp.Lat > lat)
{
gpollistRT.Add(gp);
}
else
{
gpollistRB.Add(gp);
}
}
gpollistRT.Sort(new LngComparer());
gpollistRB.Sort(new Lng2Comparer());
gpollistR.Clear();
List<PointLatLng> gpollistLT = new List<PointLatLng>();
List<PointLatLng> gpollistLB = new List<PointLatLng>();
foreach (var gp in gpollistL)
{
if (gp.Lat > lat)
{
gpollistLT.Add(gp);
}
else
{
gpollistLB.Add(gp);
}
}
//gpollistLT.Sort(new LngComparer());
gpollistLB.Sort(new Lng2Comparer());
gpollistLT.Sort(new LngComparer());
gpollistL.Clear();
gpollist.AddRange(gpollistRT);
gpollist.AddRange(gpollistRB);
gpollist.AddRange(gpollistLB);
gpollist.AddRange(gpollistLT);
GMapPolygon gpol = new GMapPolygon(gpollist, "pol");
gpol.Stroke = new Pen(Color.Red, 1);
markers.Polygons.Add(gpol);
}
public static GMap.NET.PointLatLng FindPointAtDistanceFrom(GMap.NET.PointLatLng startPoint, double initialBearingRadians, double distanceKilometres)
{
const double radiusEarthKilometres = 6371.01;
var distRatio = distanceKilometres / radiusEarthKilometres;
var distRatioSine = Math.Sin(distRatio);
var distRatioCosine = Math.Cos(distRatio);
var startLatRad = DegreesToRadians(startPoint.Lat);
var startLonRad = DegreesToRadians(startPoint.Lng);
var startLatCos = Math.Cos(startLatRad);
var startLatSin = Math.Sin(startLatRad);
var endLatRads = Math.Asin((startLatSin * distRatioCosine) + (startLatCos * distRatioSine * Math.Cos(initialBearingRadians)));
var endLonRads = startLonRad + Math.Atan2(
Math.Sin(initialBearingRadians) * distRatioSine * startLatCos,
distRatioCosine - startLatSin * Math.Sin(endLatRads));
return new GMap.NET.PointLatLng(RadiansToDegrees(endLatRads), RadiansToDegrees(endLonRads));
}
public static double DegreesToRadians(double degrees)
{
const double degToRadFactor = Math.PI / 180;
return degrees * degToRadFactor;
}
public static double RadiansToDegrees(double radians)
{
const double radToDegFactor = 180 / Math.PI;
return radians * radToDegFactor;
}
and this class
class LngComparer : IComparer<PointLatLng>
{
#region IComparer Members
public int Compare(PointLatLng x, PointLatLng y)
{
if (x == null || y == null)
throw new ArgumentException("At least one argument is null");
if (x.Lng == y.Lng)
{
if (x.Lat > y.Lat)
{
return 1;
}
else if (x.Lat < y.Lat)
{
return -1;
}
else
{
return 0;
}
}
if (x.Lng < y.Lng) return -1;
return 1;
}
#endregion
}
class Lng2Comparer : IComparer<PointLatLng>
{
#region IComparer Members
public int Compare(PointLatLng x, PointLatLng y)
{
if (x == null || y == null)
throw new ArgumentException("At least one argument is null");
if (x.Lng == y.Lng)
{
if (x.Lat > y.Lat)
{
return 1;
}
else if (x.Lat > y.Lat)
{
return -1;
}
else
{
return 0;
}
}
if (x.Lng > y.Lng) return -1;
return 1;
}
#endregion
}
I hit the same problem and on entry I had Lon, Lat and radius, here is my solution. It works like a charm :)
private void CreateCircle(Double lat, Double lon, double radius)
{
PointLatLng point = new PointLatLng(lat, lon);
int segments = 1000;
List<PointLatLng> gpollist = new List<PointLatLng>();
for (int i = 0; i < segments; i++)
gpollist.Add(FindPointAtDistanceFrom(point, i, radius / 1000));
GMapPolygon gpol = new GMapPolygon(gpollist, "pol");
markers.Polygons.Add(gpol);
}
public static GMap.NET.PointLatLng FindPointAtDistanceFrom(GMap.NET.PointLatLng startPoint, double initialBearingRadians, double distanceKilometres)
{
const double radiusEarthKilometres = 6371.01;
var distRatio = distanceKilometres / radiusEarthKilometres;
var distRatioSine = Math.Sin(distRatio);
var distRatioCosine = Math.Cos(distRatio);
var startLatRad = DegreesToRadians(startPoint.Lat);
var startLonRad = DegreesToRadians(startPoint.Lng);
var startLatCos = Math.Cos(startLatRad);
var startLatSin = Math.Sin(startLatRad);
var endLatRads = Math.Asin((startLatSin * distRatioCosine) + (startLatCos * distRatioSine * Math.Cos(initialBearingRadians)));
var endLonRads = startLonRad + Math.Atan2(
Math.Sin(initialBearingRadians) * distRatioSine * startLatCos,
distRatioCosine - startLatSin * Math.Sin(endLatRads));
return new GMap.NET.PointLatLng(RadiansToDegrees(endLatRads), RadiansToDegrees(endLonRads));
}
public static double DegreesToRadians(double degrees)
{
const double degToRadFactor = Math.PI / 180;
return degrees * degToRadFactor;
}
public static double RadiansToDegrees(double radians)
{
const double radToDegFactor = 180 / Math.PI;
return radians * radToDegFactor;
}
call
CreateCircle(51.640980, -2.673544, 1143.899431);
private void CreateCircle(Double lat, Double lon, double radius, int ColorIndex)
{
PointLatLng point = new PointLatLng(lat, lon);
int segments = 1080;
List<PointLatLng> gpollist = new List<PointLatLng>();
for (int i = 0; i < segments; i++)
{
gpollist.Add(FindPointAtDistanceFrom(point, i*(Math.PI/180), radius / 1000));
}
GMapPolygon polygon = new GMapPolygon(gpollist, "Circle");
switch (ColorIndex) {
case 1:
polygon.Fill = new SolidBrush(Color.FromArgb(80, Color.Red));
break;
case 2:
polygon.Fill = new SolidBrush(Color.FromArgb(80, Color.Orange));
break;
case 3:
polygon.Fill = new SolidBrush(Color.FromArgb(20, Color.Aqua));
break;
default:
MessageBox.Show("No search zone found!");
break;
}
polygon.Stroke = new Pen(Color.Red, 1);
markers.Polygons.Add(polygon);
gMapCtl.Overlays.Add(markers);
}
public static GMap.NET.PointLatLng FindPointAtDistanceFrom(GMap.NET.PointLatLng startPoint, double initialBearingRadians, double distanceKilometres)
{
const double radiusEarthKilometres = 6371.01;
var distRatio = distanceKilometres / radiusEarthKilometres;
var distRatioSine = Math.Sin(distRatio);
var distRatioCosine = Math.Cos(distRatio);
var startLatRad = DegreesToRadians(startPoint.Lat);
var startLonRad = DegreesToRadians(startPoint.Lng);
var startLatCos = Math.Cos(startLatRad);
var startLatSin = Math.Sin(startLatRad);
var endLatRads = Math.Asin((startLatSin * distRatioCosine) + (startLatCos * distRatioSine * Math.Cos(initialBearingRadians)));
var endLonRads = startLonRad + Math.Atan2(Math.Sin(initialBearingRadians) * distRatioSine * startLatCos,distRatioCosine - startLatSin * Math.Sin(endLatRads));
return new GMap.NET.PointLatLng(RadiansToDegrees(endLatRads), RadiansToDegrees(endLonRads));
}
public static double DegreesToRadians(double degrees)
{
const double degToRadFactor = Math.PI/180;
return degrees * degToRadFactor;
}
public static double RadiansToDegrees(double radians)
{
const double radToDegFactor = 180/Math.PI;
return radians * radToDegFactor;
}
public static double DistanceTwoPoint(double startLat, double startLong, double endLat, double endLong) {
var startPoint = new GeoCoordinate(startLat, startLong);
var endPoint = new GeoCoordinate(endLat, endLong);
return startPoint.GetDistanceTo(endPoint);
}
The only way that I am aware of that can achieve such a result is to create a list with PointLatLng points and draw them as a polygon. Here is an example:
private void CreateCircle(PointF point, double radius, int segments)
{
List<PointLatLng> gpollist = new List<PointLatLng>();
double seg = Math.PI * 2 / segments;
int y = 0;
for (int i = 0; i < segments; i++)
{
double theta = seg * i;
double a = point.X + Math.Cos(theta) * radius;
double b = point.Y + Math.Sin(theta) * radius;
PointLatLng gpoi = new PointLatLng(a,b);
gpollist.Add(gpoi);
}
GMapPolygon gpol = new GMapPolygon(gpollist, "pol");
overlayOne.Polygons.Add(gpol);
}
private void CreateCircle(PointF point, double radius, int segments)
{
List<PointLatLng> gpollist = new List<PointLatLng>();
double seg = Math.PI * 2 / segments;
int y = 0;
for (int i = 0; i < segments; i++)
{
double theta = seg * i;
double a = point.x + Math.cos( theta ) * radius;
double b = point.y + Math.sin( theta ) * radius;
PointLatLng gpoi = new PointLatLng(a,b);
gpollist.Add(gpoi);
}
GMapPolygon gpol = new GMapPolygon(gpollist, "pol");
overlayOne.Polygons.Add(gpol);
}`enter code here`
so the apartment is not going ellipses