将标记从它们所在的位置移动特定距离(以米为单位)

Move markers by a specific distance in meters from where they are

我收集了一些物品,其中一些可能具有相同的坐标。
结果,它们在 Google 地图中显示为 1 个标记,因为这些标记被绘制在彼此之上。
为了解决这个问题,我尝试 "move" 将这些标记移开几米,以免标记发生碰撞。
我想将它们移动到距离它们所在位置 5 米的地方。
我在另一个 SO answer 之后做了以下操作:

double newLat = item.getLatitude() + (Math.random() - .005) / 15000;    
double newLon = item.getLongitude() + (Math.random() - .005) / 15000;    

问题是这会稍微移动一些物品,但似乎有些物品移动了 4 米,其他物品移动了 3 米,如果可能的话,我想确保我在 4-6 米之间(min/max )
我怎样才能改变我的公式来做到这一点?

我认为最好的选择是使用 SphericalUtil. computeOffset method from the Google Maps Android API Utility Library:

computeOffset

public static LatLng computeOffset(LatLng from,
                               double distance,
                               double heading)

Returns the LatLng resulting from moving a distance from an origin in the specified heading (expressed in degrees clockwise from north).

Parameters:

from - The LatLng from which to start.

distance - The distance to travel.

heading - The heading in degrees clockwise from north.

在您的情况下,您可以将 distance 设置为 5 米,并将 heading 参数随机化为 0 到 360 度之间的值:

Random r = new Random();
int randomHeading = r.nextInt(360);
LatLng newLatLng = SphericalUtil.computeOffset(oldLatLng, 5, randomHeading);