Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

258
Views
Calculate the distance between two cities using latitude and longitude

I am calculating the distance between two cities in kilometers using their latitude and longitude. According to the Google map, the distance is 33 km but the number I am getting is 1038 km.

I understand Google gives the actual distance whereas the haversine formula used in my code tells the distance as the crow flies.

Can someone please help me understand why there is such a huge difference between both numbers?

let cities = [
    { city: "St Paul", lat: 44.9537, lon: 93.09 },
    { city: "Stillwater", lat: 36.1156, lon: 97.0584 },
];
function distance(lat1, lon1, lat2, lon2) {
    var p = 0.017453292519943295;
    var c = Math.cos;
    var a = 0.5 - c((lat2 - lat1) * p) / 2 + (c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p))) / 2;
    return 12742 * Math.asin(Math.sqrt(a));
}
let d = distance(cities[0].lat, cities[0].lon, cities[1].lat, cities[1].lon);
document.write(d.toFixed(1) + " km");

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I've verified the 1038km from another site https://www.movable-type.co.uk/scripts/latlong.html

You can also roughly see the distance by the scale marker (and it's roughly 1000km).

Your 33km is wrong, or you've got the wrong long/lat from somewhere (another town of the same name?).

Google Map reference with distance measured from movable-type script

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!