Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

253
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda