java - 如何在圆形范围内的android map 上生成标记

标签 java android dictionary area

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

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

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

最佳答案

好像是一道数学题。

圆的公式是 (x, y) = (cos(angle) * rX, sin(angle) * 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;
...

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


附加说明。

如果 mMap 表示 Google Maps map ,您可能对 Google Maps - Shape - Circle 感兴趣

关于java - 如何在圆形范围内的android map 上生成标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53443991/

相关文章:

java - 如何停止在 CollapsingToolbarLayout 上滚动,使其不会完全折叠

Python Dict 截断一个键

javascript从数组创建字典

java - 如何确定这两种算法的空间和时间复杂度?

java - SSL 握手失败时的自定义错误

java - 在 Java 中的 Selenium 中选择所选 WebElement 上方的 div

android - recyclerview 定期 ui 子更新

android - 从大文本搜索优化

android - Jetpack Compose 中 @Preview 的 isInEditMode 模拟

C++ - std::map.insert() 段错误