I have four fields lat1,lng1,lat2,lng2 and I would like to calculate the distance in km between them (rounded to 1 deciaml place).
For example table:
-----lat1-----|--- lng1 ----| ----lat2----|---lng2----|
53.4603045 | 9,98243 | 53,4470272 | 9,9956593 |
I have tried:
SELECT ST_Distance(POINT(lat1, lng1),POINT(lat2, lng2)) as distance FROM table
What do I have to change?
PostGIS is the answer.
I'll give it a go
If your postgres comes with plv8 extension (javascript), you can do something like this:
CREATE OR REPLACE FUNCTION distance_example(_la integer, _lb integer) RETURNS
real AS $$
var locations = plv8.execute("SELECT * FROM location WHERE id IN ($1, $2)", [_la, _lb]);
var x = locations[0].longitude - locations[1].longitude;
var y = locations[0].latitude - locations[1].latitude;
return Math.sqrt(x * x + y * y);
$$ LANGUAGE plv8 IMMUTABLE STRICT;
select distance_example();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.