From c76252b79c50a3c83ffb4b4f663ba5859696e2df Mon Sep 17 00:00:00 2001
From: Erwin Ried <1091420+eried@users.noreply.github.com>
Date: Wed, 6 May 2020 16:28:38 +0200
Subject: [PATCH] Replacing the linear "aproximated" way with a proper one
 (#14)

---
 firmware/application/ui/ui_geomap.cpp | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/firmware/application/ui/ui_geomap.cpp b/firmware/application/ui/ui_geomap.cpp
index a750c5fb..d7a07b36 100644
--- a/firmware/application/ui/ui_geomap.cpp
+++ b/firmware/application/ui/ui_geomap.cpp
@@ -30,6 +30,7 @@
 using namespace portapack;
 
 #include "string_format.hpp"
+#include "complex.hpp"
 
 namespace ui {
 
@@ -193,7 +194,13 @@ void GeoMap::move(const float lon, const float lat) {
 	
 	// Using WGS 84/Pseudo-Mercator projection
 	x_pos = map_width * (lon_+180)/360  - (map_rect.width() / 2);
-	y_pos = (0.5-lat_/(340.1206913+-4.21807e-5*pow((double)abs(lat_),3.4198394))) * map_height -(map_rect.height() / 1) + 32;
+
+	// Latitude calculation based on https://stackoverflow.com/a/10401734/2278659
+	double map_bottom = sin(-85.05 * pi / 180); // Map bitmap only goes from about -85 to 85 lat
+	double lat_rad = sin(lat * pi / 180);
+	double map_world_lon = map_width / (2 * pi); 
+    double map_offset = (map_world_lon / 2 * log((1 + map_bottom) / (1 - map_bottom)));
+	y_pos = map_height - ((map_world_lon / 2 * log((1 + lat_rad) / (1 - lat_rad))) - map_offset) - 128; // Offset added for the GUI
 
 	// Cap position
 	if (x_pos > (map_width - map_rect.width()))
-- 
GitLab