计算多个纬度/经度坐标对的中心点

问题描述:

给定一组纬度和经度点,我如何计算该组中心点(也就是将视图集中在所有点上的点)的纬度和经度?

Given a set of latitude and longitude points, how can I calculate the latitude and longitude of the center point of that set (aka a point that would center a view on all points)?

我使用过的Python解决方案:

Python solution I've used:

Convert lat/lon (must be in radians) to Cartesian coordinates for each location.
X = cos(lat) * cos(lon)
Y = cos(lat) * sin(lon)
Z = sin(lat)

Compute average x, y and z coordinates.
x = (x1 + x2 + ... + xn) / n
y = (y1 + y2 + ... + yn) / n
z = (z1 + z2 + ... + zn) / n

Convert average x, y, z coordinate to latitude and longitude.
Lon = atan2(y, x)
Hyp = sqrt(x * x + y * y)
Lat = atan2(z, hyp)

对它们进行平均的简单方法是,当它们从359'换回0'时,它们的边缘情况具有怪异的角度.

The simple approach of just averaging them has weird edge cases with angles when they wrap from 359' back to 0'.

关于SO的问题问有关找到一组指南针角度的平均值的问题.

A much earlier question on SO asked about finding the average of a set of compass angles.

在那里推荐的球形坐标方法的扩展将是:

An expansion of the approach recommended there for spherical coordinates would be:

  • 将每个经纬度对转换为单位长度的3D向量.
  • 对每个向量求和
  • 归一化结果向量
  • 转换回球坐标