I get latitude, longitude and altitude using android.location.Location
, after that I send this data on the server, and save it using:
update users set location = ST_SetSRID(ST_MakePoint(-40, 40, 40), 4326) where id = 1;
users.location
column was created using:
SELECT AddGeometryColumn ('users', 'location', 4326, 'POINT', 3);
I already saw an answers about 2D points, or answers in which you need to translate one SRID into another, and this is confused me. I want to know correct way from the Android to the postgis.
So, how can I calculate distance between user with id 1 and user with id 2 in kilometers using postgis?
UPD:
SELECT AddGeometryColumn ('users', 'location', 4326, 'POINT', 2);
update users set location = ST_SetSRID(ST_MakePoint(-118.4079, 33.9434), 4326) where id = 1;
update users set location = ST_SetSRID(ST_MakePoint(2.5559, 49.0083), 4326) where id = 2;
SELECT ST_Distance(a.location, b.location)/1000 as Dist_deg from users a, users b where a.id=1 AND b.id=2;
Output:
0.121898285970107
This is wrong(since these are degrees), so I tried this:
SELECT ST_Distance(ST_Transform(a.location, 2163), ST_Transform(b.location, 2163))/1000 as Dist_deg from users a, users b where a.id=1 AND b.id=2;
Output:
8731.12032614592
So, this is wrong too.
ST_3DDistance
is what you are looking for. careful, it returns distance in CRS units, so in (useless) degrees for EPSG:4326. you need to find a local projection for your area of interest, suitable for distance measurements and units in meter.