Google围绕该点随机创建标记

时间:2019-06-02 16:59:25

标签: java android google-maps google-maps-markers

我有这段代码,如何修改它以便在设定点周围创建标记?

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 :(得分:0)

生成高达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);