且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-30 13:01:34

我认为***的选择可能是使用 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);