Generate arbitrary LatLng specified device location and radius

I am trying to create random points on a map near this place. I have a form circlethat surrounds a user's location with a radius of 100, and I would like to generate random LatLng coordinates in this area of ​​the circle. So far I have come up with the following function, but point markers still appear outside the range of the environment.

    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));

    }
+4
source share
2 answers

using this https://gis.stackexchange.com/a/68275

I can create a function that generates a random LatLng point in a specific radius, where the radius is in a meter.

public LatLng getRandomLocation(LatLng point, int radius) {

        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 = 0; i<10; 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));
        }
        //Get nearest point to the centre
        int indexOfNearestPointToCentre = randomDistances.indexOf(Collections.min(randomDistances));
        return randomPoints.get(indexOfNearestPointToCentre);
    }

for - , , , , . .

+6

. , .

  // Convert radius from meters to degrees
double radiusInDegrees = radius / 111000f;

. .

+1

Source: https://habr.com/ru/post/1617854/


All Articles