Skip to content

Commit 0c402d0

Browse files
EosBandimeee1
authored andcommitted
Fix map drag around the poles (projection singularity)
1 parent 3d819e6 commit 0c402d0

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@ docs/
4646
obj2/
4747
obj3/
4848

49+
/.claude/settings.local.json
50+
/CLAUDE.md

GCSViews/FlightData.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public partial class FlightData : MyUserControl, IActivate, IDeactivate
5555
internal GMapMarker CurrentGMapMarker;
5656

5757
internal PointLatLng MouseDownStart;
58+
internal Point MouseDownStartLocal;
5859

5960
//The file path of the selected script
6061
internal string selectedscript = "";
@@ -2933,6 +2934,7 @@ private void gMapControl1_Click(object sender, EventArgs e)
29332934
private void gMapControl1_MouseDown(object sender, MouseEventArgs e)
29342935
{
29352936
MouseDownStart = gMapControl1.FromLocalToLatLng(e.X, e.Y);
2937+
MouseDownStartLocal = e.Location;
29362938
Console.WriteLine("gMapControl1_MouseDown "+ MouseDownStart);
29372939

29382940
if (ModifierKeys == Keys.Control)
@@ -2971,13 +2973,26 @@ private void gMapControl1_MouseMove(object sender, MouseEventArgs e)
29712973
{
29722974
if (e.Button == MouseButtons.Left)
29732975
{
2974-
PointLatLng point = gMapControl1.FromLocalToLatLng(e.X, e.Y);
2976+
// incremental delta avoids direction lock near pole/singularity regions
2977+
int dx = e.X - MouseDownStartLocal.X;
2978+
int dy = e.Y - MouseDownStartLocal.Y;
29752979

2976-
double latdif = MouseDownStart.Lat - point.Lat;
2977-
double lngdif = MouseDownStart.Lng - point.Lng;
2980+
if (dx == 0 && dy == 0)
2981+
return;
2982+
2983+
double absLat = Math.Abs(gMapControl1.Position.Lat);
2984+
PointLatLng newCenter = gMapControl1.FromLocalToLatLng(
2985+
gMapControl1.Width / 2 - dx,
2986+
gMapControl1.Height / 2 - dy);
2987+
2988+
if (!double.IsNaN(newCenter.Lat) && !double.IsNaN(newCenter.Lng) &&
2989+
!double.IsInfinity(newCenter.Lat) && !double.IsInfinity(newCenter.Lng))
2990+
{
2991+
gMapControl1.Position = newCenter;
2992+
}
29782993

2979-
gMapControl1.Position = new PointLatLng(center.Position.Lat + latdif,
2980-
center.Position.Lng + lngdif);
2994+
// consume this step (critical for stable drag)
2995+
MouseDownStartLocal = e.Location;
29812996
}
29822997
else
29832998
{

GCSViews/FlightPlanner.cs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ private void but_mincommands_Click(object sender, System.EventArgs e)
125125
private DateTime mapupdate = DateTime.MinValue;
126126
private string mobileGpsLog = string.Empty;
127127
private PointLatLng MouseDownStart;
128+
private Point MouseDownStartLocal;
128129
private PointLatLngAlt mouseposdisplay = new PointLatLngAlt(0, 0);
129130
private WPOverlay wpOverlay;
130131
private bool polygongridmode;
@@ -1035,7 +1036,9 @@ public void redrawPolygonSurvey(List<PointLatLngAlt> list)
10351036
if (now == null || next == null)
10361037
continue;
10371038

1038-
var mid = new PointLatLngAlt((now.Lat + next.Lat) / 2, (now.Lng + next.Lng) / 2, 0);
1039+
var p1 = MainMap.FromLatLngToLocal(now);
1040+
var p2 = MainMap.FromLatLngToLocal(next);
1041+
var mid = new PointLatLngAlt(MainMap.FromLocalToLatLng((int)((p1.X + p2.X) / 2), (int)((p1.Y + p2.Y) / 2)));
10391042

10401043
var pnt = new GMapMarkerPlus(mid);
10411044
pnt.Tag = new midline() {now = now, next = next};
@@ -1480,8 +1483,10 @@ public void writeKML()
14801483
if (now == null || next == null)
14811484
continue;
14821485

1483-
var mid = new PointLatLngAlt((now.Lat + next.Lat) / 2, (now.Lng + next.Lng) / 2,
1484-
(now.Alt + next.Alt) / 2);
1486+
var p1 = MainMap.FromLatLngToLocal(now);
1487+
var p2 = MainMap.FromLatLngToLocal(next);
1488+
var mid = new PointLatLngAlt(MainMap.FromLocalToLatLng((int)((p1.X + p2.X) / 2), (int)((p1.Y + p2.Y) / 2)));
1489+
mid.Alt = (now.Alt + next.Alt) / 2;
14851490

14861491
var pnt = new GMapMarkerPlus(mid);
14871492
pnt.Tag = new midline() {now = now, next = next};
@@ -1557,7 +1562,9 @@ public void writeKML()
15571562
if (now == null || next == null)
15581563
continue;
15591564

1560-
var mid = new PointLatLngAlt((now.Lat + next.Lat) / 2, (now.Lng + next.Lng) / 2, 0);
1565+
var p1 = MainMap.FromLatLngToLocal(now);
1566+
var p2 = MainMap.FromLatLngToLocal(next);
1567+
var mid = new PointLatLngAlt(MainMap.FromLocalToLatLng((int)((p1.X + p2.X) / 2), (int)((p1.Y + p2.Y) / 2)));
15611568

15621569
var pnt = new GMapMarkerPlus(mid);
15631570
pnt.Tag = new midline() {now = now, next = next};
@@ -7357,6 +7364,7 @@ private void MainMap_MouseDown(object sender, MouseEventArgs e)
73577364
return;
73587365

73597366
MouseDownStart = MainMap.FromLocalToLatLng(e.X, e.Y);
7367+
MouseDownStartLocal = e.Location;
73607368

73617369
// Console.WriteLine("MainMap MD");
73627370

@@ -7526,16 +7534,34 @@ private void MainMap_MouseMove(object sender, MouseEventArgs e)
75267534
}
75277535
else // left click pan
75287536
{
7529-
double latdif = MouseDownStart.Lat - point.Lat;
7530-
double lngdif = MouseDownStart.Lng - point.Lng;
7531-
75327537
try
75337538
{
75347539
lock (thisLock)
75357540
{
75367541
if (!isMouseClickOffMenu)
7537-
MainMap.Position = new PointLatLng(center.Position.Lat + latdif,
7538-
center.Position.Lng + lngdif);
7542+
{
7543+
// incremental delta avoids direction lock when crossing pole singularities
7544+
int dx = e.X - MouseDownStartLocal.X;
7545+
int dy = e.Y - MouseDownStartLocal.Y;
7546+
7547+
if (dx == 0 && dy == 0)
7548+
return;
7549+
7550+
double absLat = Math.Abs(MainMap.Position.Lat);
7551+
7552+
PointLatLng newCenter = MainMap.FromLocalToLatLng(
7553+
MainMap.Width / 2 - dx,
7554+
MainMap.Height / 2 - dy);
7555+
7556+
if (!double.IsNaN(newCenter.Lat) && !double.IsNaN(newCenter.Lng) &&
7557+
!double.IsInfinity(newCenter.Lat) && !double.IsInfinity(newCenter.Lng))
7558+
{
7559+
MainMap.Position = newCenter;
7560+
}
7561+
7562+
// key part: consume this step so next move is relative to current state
7563+
MouseDownStartLocal = e.Location;
7564+
}
75397565
}
75407566
}
75417567
catch (Exception ex)

0 commit comments

Comments
 (0)