I'm developing a project using LeafleatJS and PostGIS. Currently I'm getting trouble while calculating distance between two coordinates. For example:
Point 1 - lat: -12.99835864475412, lng: -38.506194949150085
Point 2 - lat: -12.999215865191118, lng: -38.50590527057648
Leaflet calculates 99.906 m, while using the following SQL in PostGIS:
SELECT ST_Distance_Sphere(
ST_GeomFromText('POINT(-12.99835864475412 -38.506194949150085)', 4326),
ST_GeomFromText('POINT(-12.999215865191118 -38.50590527057648)', 4326));
I get 81.248 m.
Leafleat uses Haversine distance formula to calculate distance and I'm using its default CRS - EPSG3857 - while on PostGIS I'm storing data as a Geographic Point with SRID 4326.
I know, it seems confusing but I'm following the answer given on this link:
All of this further confused by that fact that often even though the map is in Web Mercator(EPSG: 3857), the actual coordinates used are in lat-long (EPSG: 4326). This convention is used in many places, such as:
- In Most Mapping API,s You can give the coordinates in Lat-long, and the API automatically transforms it to the appropriate Web Mercator coordinates.
I'm new on GIS so... why there's a difference between the results (I know they use different formulas to calculate, but almost 20m difference is pretty considerable)? Am I storing and retrieving data in the wrong way? Which one is the most accurate (Leafleat or PostGIS)?