使用R计算两点(lat,long)之间地理空间距离的函数

Function to calculate geospatial distance between two points (lat,long) using R

我有经纬度格式的地理编码点,我想使用 R 计算它们之间的距离。这看起来很简单,但我找不到可以轻松完成的函数。我一直在尝试使用 gdistance 包来完成它,但它看起来非常复杂并且面向图形,我只需要一个数字。 distanceBetween(pointA,pointB) 之类的东西 returns 一个数字。

加载geosphere包你可以使用一些不同的函数

library(geosphere)
distm(c(lon1, lat1), c(lon2, lat2), fun = distHaversine)

另外:

distHaversine()
distMeeus()
distRhumb()
distVincentyEllipsoid()
distVincentySphere()

...

同意@PereG 的上述回答,但认为纬度和经度的顺序是相反的:lon、lat。这将影响距离矩阵的结果。所以正确的是:

library(geosphere)
distm (c(lon1, lat1), c(lon2, lat2), fun = distHaversine)

来源:ftp://cran.r-project.org/pub/R/web/packages/geosphere/geosphere.pdf