postgresqlgeolocation

PostgreSQL calculate distance between two points without using PostGIS


How to calculate distance between two points when latitude and longitude are in two separate columns in a table?
I cannot use PostGIS because I use heroku Postgres free version.


Solution

  • You can using, something like this:

    select SQRT(POW(69.1 * (latitude::float -  p_lat::float), 2) + 
        POW(69.1 * (p_lon::float - longitude::float) * COS(latitude::float / 57.3), 2)
    )
    

    In this:

    (latitude, Longitude) point 1.

    (p_lat, p_lon) point 2