Hi,
I’m having trouble getting the Haversine formula to work properly in MIT App Inventor. The calculated distance between two map points is always a bit larger than the real value.
I’ve noticed that even at the very beginning of the code, the conversion from degrees to radians is not accurate. I suspect the main issue is the limited number of decimal places – only 5 are used, which might be causing rounding errors.
I’m attaching a screenshot and a working HTML page. On the HTML page, you can enter coordinates to check the distance, and it matches values from Google Maps, so I know the formula itself is correct.
I’m trying to use the same algorithm in MIT App Inventor, but something isn’t working.
Could someone please help me find a solution to this problem?
To make it easier to solve the problem, I’ll mention right away that, for example, the distance between the coordinates 0,0 and 45,45 degrees is 6679 km.
The accuracy of the calculations can also be checked online on this website HAVERSINE
Thank you!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Haversine - English Variable Names</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
.result, .vars { font-size: 1.1em; margin-top: 20px; }
code { background: #f8f8f8; padding: 2px 4px; border-radius: 3px; }
.vars td { padding: 2px 10px; border-bottom: 1px solid #eee; }
table { border-collapse: collapse; margin-top: 10px;}
</style>
</head>
<body>
<h2>Distance calculation and intermediate variables (haversine, English variable names)</h2>
<p>You can change the coordinates inside the JavaScript code.</p>
<div class="result" id="result"></div>
<div class="vars" id="vars"></div>
<script>
// --- CHANGE COORDINATES HERE ---
const newLatitude = 0; // lat
const newLongitude = 0; // lon
const oldLatitude = 45; // lat_old
const oldLongitude = 45; // lon_old
function toRadians(degrees) {
return degrees * Math.PI / 180;
}
const oldLatRad = toRadians(oldLatitude);
const newLatRad = toRadians(newLatitude);
const deltaLat = toRadians(newLatitude - oldLatitude);
const deltaLon = toRadians(newLongitude - oldLongitude);
const earthRadius = 6371; // Earth's radius in km
//const a = Math.sin(deltaLat / 2) ** 2 +
// Math.cos(oldLatRad) * Math.cos(newLatRad) *
// Math.sin(deltaLon / 2) ** 2;
const a1 = Math.sin(deltaLat / 2) ** 2;
const a2 = Math.cos(oldLatRad) * Math.cos(newLatRad);
const a3 = Math.sin(deltaLon / 2) ** 2;
const a = a1 + a2 * a3;
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const distance = earthRadius * c;
document.getElementById("result").innerHTML =
`Distance between points:<br>
<code>oldLatitude=${oldLatitude}, oldLongitude=${oldLongitude}</code><br>
<code>newLatitude=${newLatitude}, newLongitude=${newLongitude}</code><br>
is <strong>${distance.toFixed(4)} km</strong> (haversine).`;
document.getElementById("vars").innerHTML =
`<h3>Intermediate variables:</h3>
<table>
<tr><td><code>oldLatRad</code></td><td>${oldLatRad.toFixed(6)} rad</td></tr>
<tr><td><code>newLatRad</code></td><td>${newLatRad.toFixed(6)} rad</td></tr>
<tr><td><code>deltaLat</code></td><td>${deltaLat.toFixed(6)} rad</td></tr>
<tr><td><code>deltaLon</code></td><td>${deltaLon.toFixed(6)} rad</td></tr>
<tr><td><code>a1</code></td><td>${a1}</td></tr>
<tr><td><code>a2</code></td><td>${a2}</td></tr>
<tr><td><code>a3</code></td><td>${a3}</td></tr>
<tr><td><code>a</code></td><td>${a.toExponential(6)}</td></tr>
<tr><td><code>c</code></td><td>${c.toFixed(6)} rad</td></tr>
<tr><td><code>distance</code></td><td>${distance.toFixed(4)} km</td></tr>
</table>`;
</script>
</body>
</html>
haver_test.aia (37.2 KB)