将标记从其所在位置移动特定距离(以米为单位) [英] Move markers by a specific distance in meters from where they are
问题描述
我有一组项目,其中一些可能具有相同的坐标.
结果,由于标记在一个位置上被绘制为一个,因此它们在Google Maps中显示为1个标记.
为了解决这个问题,我尝试将这些标记移动"几米,以使标记不会发生碰撞.
我想将它们移动到距离其位置5米的位置.
我在另一个 SO答案之后做了以下操作:
I have a collection of items and some of them may have the same coordinates.
As a result they are displayed as 1 marker in Google Maps since the markers are painted one on top of each other.
To address this I tried to "move" those markers by a few meters so that the markers do not collide.
I would like to move them to a 5 meters from where their location is.
I did the following following another SO answer:
double newLat = item.getLatitude() + (Math.random() - .005) / 15000;
double newLon = item.getLongitude() + (Math.random() - .005) / 15000;
问题是,这会使物品稍微移动了一些,但似乎有些物品移动了4m,另一些物品又移动了3m,因此,我希望尽可能确保我在4-6米(最小/最大)之间.
如何更改我的公式来做到这一点?
The problem is that this moves the items a bit but it seems that some are moved by 4m others by 3m and I would like if possible to ensure that I will be between 4-6 meters (min/max)
How can I change my formula to do this?
推荐答案
我认为最好的选择可能是使用 Google Maps Android API实用程序库:
I think that the best option could be using the SphericalUtil. computeOffset
method from the Google Maps Android API Utility Library:
computeOffset
public static LatLng computeOffset(LatLng from,
double distance,
double heading)
返回LatLng,该LatLng是由于距指定方向的原点(从北向顺时针以度为单位)移动了一定距离而产生的.
Returns the LatLng resulting from moving a distance from an origin in the specified heading (expressed in degrees clockwise from north).
参数:
来源-开始的LatLng.
from - The LatLng from which to start.
距离-行驶距离.
航向-北向顺时针旋转的航向.
heading - The heading in degrees clockwise from north.
根据您的情况,您可以将distance
设置为5米,并将heading
参数随机化为介于0和360度之间的某个值:
In your case you can set the distance
to be 5 meters and randomise the heading
parameter to be something between 0 and 360 degrees:
Random r = new Random();
int randomHeading = r.nextInt(360);
LatLng newLatLng = SphericalUtil.computeOffset(oldLatLng, 5, randomHeading);
这篇关于将标记从其所在位置移动特定距离(以米为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!