给定设备位置和半径,生成随机LatLng [英] Generate random LatLng given device location and radius

查看:74
本文介绍了给定设备位置和半径,生成随机LatLng的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在地图上给定位置附近生成随机点.我有一个circle形状,它以100的半径围绕用户位置,并且我想在该圆形区域内生成随机的LatLng坐标.到目前为止,我已经提出了以下功能,但是点标记仍然出现在圆范围之外.

I am trying to generate random points on the map near a given location. I have a circle shape which surrounds the user location with a radius of 100, and I would like to generate random LatLng coordinates within this circle area. I have come up with the following function so far, but the point markers are still appearing outside the circle range.

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

    }

推荐答案

在此帮助下 https://gis.stackexchange.com/a/68275

我能够创建一个函数来生成一定半径内的随机LatLng点,其中半径以米为单位.

I am able to make a function which generate random LatLng point within certain radius, where radius is in 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循环的目的只是为了确保获得最接近的随机点,因为我看到随着半径的增加,点逐渐偏离圆.您可以删除循环.

The purpose of for loop is just to ensure to get nearest random point, as I have seen points were getting out of circle as I am increasing the radius. You may remove loop.

这篇关于给定设备位置和半径,生成随机LatLng的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆