private void map_MouseMove(object sender, MouseEventArgs e)
{
var mousecurrent = map.FromLocalToLatLng(e.X, e.Y);
if (mousedown)
{
mousedragging = true;
if (currentmode == mode.panmode)
{
if (e.Button == MouseButtons.Left)
{
double latdif = mousestart.Lat - mousecurrent.Lat;
double lngdif = mousestart.Lng - mousecurrent.Lng;
try
{
map.Position = new PointLatLng(map.Position.Lat + latdif, map.Position.Lng + lngdif);
}
catch { }
}
}
else if (currentmode == mode.drawbox)
{
if (e.Button == MouseButtons.Left)
{
var rect = RectangleF.FromLTRB((float)mousestart.Lng, (float)mousestart.Lat, (float)mousecurrent.Lng, (float)mousecurrent.Lat);
list.Clear();
// tl
list.Add(mousestart);
// tr
list.Add(new PointLatLng(rect.Top, rect.Right));
// br
list.Add(mousecurrent);
// bl
list.Add(new PointLatLng(rect.Bottom, rect.Left));
if (boxpoly != null)
layerpolygons.Polygons.Remove(boxpoly);
boxpoly = null;
boxpoly = new GMapPolygon(list, "boxpoly");
boxpoly.IsHitTestVisible = true;
boxpoly.Stroke = new Pen(Color.Red, 2);
boxpoly.Fill = Brushes.Transparent;
layerpolygons.Polygons.Add(boxpoly);
map.Invalidate();
}
}
else if (currentmode == mode.movebox)
{
//if (mouseinsidepoly)
{
double latdif = mousestart.Lat - mousecurrent.Lat;
double lngdif = mousestart.Lng - mousecurrent.Lng;
for (int a = 0; a < boxpoly.Points.Count; a++)
{
boxpoly.Points[a] = new PointLatLng(boxpoly.Points[a].Lat - latdif, boxpoly.Points[a].Lng - lngdif);
}
UpdateListFromBox();
map.UpdatePolygonLocalPosition(boxpoly);
map.Invalidate();
mousestart = mousecurrent;
}
}
else if (currentmode == mode.editbox)
{
double latdif = mousestart.Lat - mousecurrent.Lat;
double lngdif = mousestart.Lng - mousecurrent.Lng;
// 2 point the create the lowest crosstrack distance
// extend at 90 degrees to the bearing of the points based on mouse position
PointLatLngAlt p0;
PointLatLngAlt p1;
PointLatLngAlt bestp0 = PointLatLngAlt.Zero;
PointLatLngAlt bestp1 = PointLatLngAlt.Zero;
double bestcrosstrack = 9999999;
double R = 6371000;
for (int a = 0; a < boxpoly.Points.Count; a++)
{
p0 = boxpoly.Points[a];
p1 = boxpoly.Points[(a + 1) % (boxpoly.Points.Count)];
var distp0p1 = p0.GetDistance(mousecurrent);
var bearingp0curr = p0.GetBearing(mousecurrent);
var bearringp0p1 = p0.GetBearing(p1);
var ct = Math.Asin(Math.Sin(distp0p1 / R) * Math.Sin((bearingp0curr - bearringp0p1) * deg2rad)) * R;
if (Math.Abs(ct) < Math.Abs(bestcrosstrack))
{
bestp0 = p0;
bestp1 = p1;
bestcrosstrack = ct;
}
}
var bearing = bestp0.GetBearing(bestp1);
layerpolygons.Markers.Clear();
layerpolygons.Markers.Add(new GMarkerGoogle(bestp0, GMarkerGoogleType.blue));
layerpolygons.Markers.Add(new GMarkerGoogle(bestp1, GMarkerGoogleType.blue));
bearing = ((PointLatLngAlt)mousestart).GetBearing(mousecurrent);
var newposp0 = bestp0.newpos(bearing, Math.Abs(bestcrosstrack));
var newposp1 = bestp1.newpos(bearing, Math.Abs(bestcrosstrack));
boxpoly.Points[boxpoly.Points.IndexOf(bestp0)] = newposp0;
boxpoly.Points[boxpoly.Points.IndexOf(bestp1)] = newposp1;
UpdateListFromBox();
map.UpdatePolygonLocalPosition(boxpoly);
map.Invalidate();
mousestart = mousecurrent;
}
}
mousedragging = false;
}