问题
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;
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?
回答1:
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)
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.
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);
来源:https://stackoverflow.com/questions/42937949/move-markers-by-a-specific-distance-in-meters-from-where-they-are