如何根据经纬度计算距离
How to calculate distance from latitude and longitude
在 Postgres 9.1+ 数据库中包含路由点:
create table doksyndm (
id serial primary key,
readingtime timestamp with time zone not null,
latitude float,
longitude float
);
使用来自 javascript 的 navigator.geolocation 调用从浏览器保存积分。
如何根据这些数据计算路线长度?
路线小于 300 公里,因此点之间的直线长度可以相加。
它应该适用于 Postgres 9.1 和更新版本。
结果应该是一个数字,点与点之间的总距离,以公里为单位。
数据库和javascript代码发送点如果合理可以重构
Calculate distance between two points in google maps V3 中的答案如何用程序语言实现。
他们使用 SQL 中没有的 for 循环。 Maby SUM() with window function can used to implement this ?
我假设您想要从最早点到下一个最早点的距离,依此类推...
从 Postgresql 9.3 开始,有一个使用 lateral join.
的优雅解决方案
首先,安装postgis扩展,这样你就可以使用它的距离功能了:
CREATE EXTENSION POSTGIS;
现在查询:
SELECT SUM(st_distance(st_point(a.longitude,a.latitude)::geography,
st_point(b.longitude,b.latitude)::geography)
) / 1000 AS distance_km FROM doksyndm a
LEFT JOIN LATERAL
(SELECT * FROM doksyndm WHERE readingtime > a.readingtime limit 1) b ON TRUE;
这个使用 distinct on 的查询应该适用于所有版本:
SELECT SUM (st_distance(st_point(alon,alat)::geography,
st_point(blon,blat)::geography))/1000 as distance_km FROM
( select distinct ON (a.readingtime) a.readingtime,
a.latitude as alat,
a.longitude as alon,
b.latitude as blat ,
b.longitude as blon
FROM doksyndm a inner join doksyndm b on a.readingtime < b.readingtime
ORDER by a.readingtime,b.readingtime) x
在 Postgres 9.1+ 数据库中包含路由点:
create table doksyndm (
id serial primary key,
readingtime timestamp with time zone not null,
latitude float,
longitude float
);
使用来自 javascript 的 navigator.geolocation 调用从浏览器保存积分。
如何根据这些数据计算路线长度? 路线小于 300 公里,因此点之间的直线长度可以相加。 它应该适用于 Postgres 9.1 和更新版本。
结果应该是一个数字,点与点之间的总距离,以公里为单位。 数据库和javascript代码发送点如果合理可以重构
Calculate distance between two points in google maps V3 中的答案如何用程序语言实现。
他们使用 SQL 中没有的 for 循环。 Maby SUM() with window function can used to implement this ?
我假设您想要从最早点到下一个最早点的距离,依此类推...
从 Postgresql 9.3 开始,有一个使用 lateral join.
的优雅解决方案首先,安装postgis扩展,这样你就可以使用它的距离功能了:
CREATE EXTENSION POSTGIS;
现在查询:
SELECT SUM(st_distance(st_point(a.longitude,a.latitude)::geography,
st_point(b.longitude,b.latitude)::geography)
) / 1000 AS distance_km FROM doksyndm a
LEFT JOIN LATERAL
(SELECT * FROM doksyndm WHERE readingtime > a.readingtime limit 1) b ON TRUE;
这个使用 distinct on 的查询应该适用于所有版本:
SELECT SUM (st_distance(st_point(alon,alat)::geography,
st_point(blon,blat)::geography))/1000 as distance_km FROM
( select distinct ON (a.readingtime) a.readingtime,
a.latitude as alat,
a.longitude as alon,
b.latitude as blat ,
b.longitude as blon
FROM doksyndm a inner join doksyndm b on a.readingtime < b.readingtime
ORDER by a.readingtime,b.readingtime) x