有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在圆形范围内的安卓地图上生成标记

我正在尝试制作一个口袋妖怪风格的应用程序。 我曾尝试使用纬度和经度公式来计算标记(口袋妖怪)产生的坐标范围,但我只得到了一个矩形区域。我想在玩家周围的圆形区域产卵,我怎么做

这是我计算矩形面积的代码

        Random r = new Random();
        int randomDistanceLatitude = r.nextInt(range*2) - range;
        r = new Random();
        int randomDistanceLongitude = r.nextInt(range*2) - range;


        double latitudeDegreeToAdd = 1.7 * randomDistanceLatitude * 0.000009043717329571146924;
        double longitudeDegreeToAdd = randomDistanceLongitude * (1 / (111320 * Math.cos(infoLocal.getLastKnownLocation().getLatitude())));

        LatLng enemy1Location = new LatLng(infoLocal.getLastKnownLocation().getLatitude() + latitudeDegreeToAdd, infoLocal.getLastKnownLocation().getLongitude() + longitudeDegreeToAdd);

        enemies[i] = mMap.addMarker(new MarkerOptions().position(enemy1Location).title("Monster").snippet("Click to Attack!").icon(BitmapDescriptorFactory.fromResource(R.drawable.enemy3)).anchor(0.5f, 0.5f));

共 (1) 个答案

  1. # 1 楼答案

    这似乎是一道数学题

    圆的公式是(x,y)=(cos(角度)*rX,sin(角度)*rY)+(centerX,centerY)

    因此,代码将如下所示

    Random r = new Random();
    double radius = r.nextFloat() * range; // or r.nextDouble()...
    double angle = r.nextFloat() * 2.0f * Math.PI;
    double randomDistanceLatitude = Math.cos(angle) * radius;
    double randomDistanceLongitude = Math.sin(angle) * radius;
    ...
    

    由于latlng不在笛卡尔坐标系中,它不是一个纯圆,但我认为在这个用例中range足够小,可以忽略这种失真


    补充说明

    如果mMap表示谷歌地图,您可能对谷歌地图-Shape-Circle感兴趣