我正在使用PostGIS在SQL查询中为我做一些lat和lon距离计算。它返回两个lat和lon位置之间的距离,但它返回的单位是当乌鸦飞过时lat和lon的距离:
"SELECT ST_Distance(ST_MakePoint(\"Users\".\"longitude\",\"Users\".\"latitude\"), ST_MakePoint($1,$2)) FROM \"Users\" WHERE \"Users\".\"username\"=$3"
示例输入:
Lat& Lon 1 - > -0.186768 51.450584
Lat& Lon 2 - > -0.572662 51.245584
给予:0.436965878800621
所以我的问题是如何将0.436965878800621
变为例如1000m
我不确定Calculate distance between 2 GPS coordinates是否对我有帮助?或者这是否在计算中过早/过晚开始。
由于
答案 0 :(得分:2)
您可以将geometry
生成的ST_MakePoint()
转换为geography
类型,ST_Distance()
将以米为单位生成输出:
SELECT ST_Distance(geography(ST_MakePoint("Users"."longitude", "Users"."latitude")),
geography(ST_MakePoint($1, $2)))
FROM "Users"
WHERE "Users"."username" = $3
答案 1 :(得分:0)
var R = 6371000; // metres
var φ1 = lat1.toRadians();
var φ2 = lat2.toRadians();
var Δφ = (lat2-lat1).toRadians();
var Δλ = (lon2-lon1).toRadians();
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
这是在javascript中。只是为了给你一个想法 - 你可以改变它。