[Python] Getting distance between two points based on latitude/longitude

Discussão em 'Python' iniciado por Stack, Agosto 1, 2025.

  1. Stack

    Stack Membro Participativo

    I tried implementing the formula in Finding distances based on Latitude and Longitude. The applet does good for the two points I am testing:

    [​IMG]

    Yet my code is not working.

    from math import sin, cos, sqrt, atan2

    R = 6373.0

    lat1 = 52.2296756
    lon1 = 21.0122287
    lat2 = 52.406374
    lon2 = 16.9251681

    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = (sin(dlat/2))**2 + cos(lat1) * cos(lat2) * (sin(dlon/2))**2
    c = 2 * atan2(sqrt(a), sqrt(1-a))
    distance = R * c

    print "Result", distance
    print "Should be", 278.546


    It returns the distance 5447.05546147. Why?

    Continue reading...

Compartilhe esta Página