我試圖在給定位置附近的地圖上生成隨機點。我有一個圍繞半徑爲100的用戶位置的circle
形狀,並且我想在此圓形區域內生成隨機的LatLng座標。到目前爲止,我已經提出了以下功能,但點標記仍然出現在圓圈範圍之外。給定設備位置和半徑生成隨機LatLng
double lat = location.getLatitude();
double lon = location.getLongitude();
for (int i = 0; i < markers.size(); i++) {
Marker mrk = markers.get(i);
Random random = new Random();
double radiusInDegrees =mCircle.getRadius();
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(lat);
double newLongitude = new_x + lon;
double newLatitude = y + lat;
mrk.setPosition(new LatLng(newLatitude,newLongitude));
}