Google картирует случайные создания маркеров вокруг точки - PullRequest
0 голосов
/ 02 июня 2019

У меня есть этот код, как я могу изменить его так, чтобы маркеры создавались вокруг заданной точки?

private List<Marker> mark = new ArrayList<Marker>();
public LatLng getRandomLocation() {
int radius = 7315;
LatLng point = new LatLng(59.945610, 30.315746);
List<LatLng> randomPoints = new ArrayList<>();
List<Float> randomDistances = new ArrayList<>();
Location myLocation = new Location("");
myLocation.setLatitude(point.latitude);
myLocation.setLongitude(point.longitude);
//This is to generate 10 random points
for(int i = 130; i<150; i++) {
double x0 = point.latitude;
double y0 = point.longitude;
Random random = new Random();
// Convert radius from meters to degrees
double radiusInDegrees = radius / 111000f;
double u = random.nextDouble();
double v = random.nextDouble();
double w = radiusInDegrees * Math.sqrt(u);
double t = 2 * Math.PI * v;
double x = w * Math.cos(t);
double y = w * Math.sin(t);
// Adjust the x-coordinate for the shrinking of the east-west distances
double new_x = x / Math.cos(y0);
double foundLatitude = new_x + x0;
double foundLongitude = y + y0;
LatLng randomLatLng = new LatLng(foundLatitude, foundLongitude);
randomPoints.add(randomLatLng);
Location l1 = new Location("");
l1.setLatitude(randomLatLng.latitude);
l1.setLongitude(randomLatLng.longitude);
randomDistances.add(l1.distanceTo(myLocation));
String[] array = getResources().getStringArray(R.array.arts);
String randomStr = array[new Random().nextInt(array.length)];
Marker marker = mMap.addMarker(new MarkerOptions()
.visible(true)
.draggable(false)
.title(randomStr)
.position(randomLatLng)
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.test)));
mark.add(marker);
Log.e("marker", String.valueOf(mark.size()));
}
//Get nearest point to the center
int indexOfNearestPointToCentre = randomDistances.indexOf(Collections.min(randomDistances));
return randomPoints.get(indexOfNearestPointToCentre);
}

Мне нужно, например, ~ 30 маркеров, чтобы создать вокруг желаемой точки, с разным разбросомнапример, в течение ~ 15 километров они создаются как будто линией с юга на север

Извините за мой плохой английский, я русский, и извините за мой плохой код, я отправляю это с телефона.

1 Ответ

0 голосов
/ 03 июня 2019

Создание случайных углов до 360 градусов.

double angle = random.nextDouble()*Math.PI*2;

Создание случайного радиуса в пределах максимального радиуса.

double randomRadius = radius*random.nextDouble();

Расчет относительной точки

double relativeX = Math.cos(angle)*randomRadius;
double relativeY = Math.sin(angle)*randomRadius;

Добавитьотносительная точка к центру

LatLng randomLatLng = new LatLng(point.latitude + relativeX, point.longitude + relativeY);
...