Android谷歌地图:添加圆形孔的多边形。

10

你好,目前我正在开发谷歌地图应用程序。

我想要实现以下效果:

图片描述

为了达到这个效果,我计划首先在该国家上创建一个多边形叠加层,然后在其中添加一个具有一定半径的孔洞,以便在缩放时它会收缩和扩展。

现在我知道如何创建多边形;

mMap.addPolygon(new PolygonOptions().addAll(sCountryBorder).fillColor(0xcc000000));

现在我想给这个多边形添加一个孔,但我不知道如何生成正确半径的圆形孔。

mMap.addPolygon(new PolygonOptions().addAll(sCountryBorder).fillColor(0xcc000000).addHole({CIRCULAR_HOLE}));

我知道在Google Maps中可以创建一个特定半径的圆,那么是否可能将其转换为LatLng对象数组?

mMap.addCircle(new CircleOptions()
                        .center(newLocation)
                        .radius(mRadius.size)
                        .strokeWidth(0)
                        .fillColor(getResources().getColor(R.color.transparant)));

请查看http://stackoverflow.com/questions/36030648/custom-disabled-and-blank-map-in-android/36039979#36039979。 - antonio
但是我如何考虑实际半径以米为单位? - sn0ep
该示例中的代码已经考虑了半径(150米)。 - antonio
我明白了,我会去查看的! - sn0ep
如果您想制作15公里标记,请参考我的答案https://stackoverflow.com/a/52325421/8740243。 - Ankit Dubariya
2个回答

21

不幸的是,Google地图库不允许获取圆形的LatLng数组。因此,您需要自己绘制一个圆。

基本上,您需要提供一个方法,用于为填充您的地图的多边形创建一个洞(圆形)。

有3个步骤。

第1步是构建一个方法,用于创建覆盖整个地图的多边形。

private static List<LatLng> createOuterBounds() {
    float delta = 0.01f;

    return new ArrayList<LatLng>() {{
        add(new LatLng(90 - delta, -180 + delta));
        add(new LatLng(0, -180 + delta));
        add(new LatLng(-90 + delta, -180 + delta));
        add(new LatLng(-90 + delta, 0));
        add(new LatLng(-90 + delta, 180 - delta));
        add(new LatLng(0, 180 - delta));
        add(new LatLng(90 - delta, 180 - delta));
        add(new LatLng(90 - delta, 0));
        add(new LatLng(90 - delta, -180 + delta));
    }};
}

第二步是创建一个方法,该方法将返回一个包含圆形中心点坐标(LatLng)的Iterable对象。

private static Iterable<LatLng> createHole(LatLng center, int radius) {
    int points = 50; // number of corners of inscribed polygon

    double radiusLatitude = Math.toDegrees(radius / (float) EARTH_RADIUS);
    double radiusLongitude = radiusLatitude / Math.cos(Math.toRadians(center.latitude));

    List<LatLng> result = new ArrayList<>(points);

    double anglePerCircleRegion = 2 * Math.PI / points;

    for (int i = 0; i < points; i++) {
        double theta = i * anglePerCircleRegion;
        double latitude = center.latitude + (radiusLatitude * Math.sin(theta));
        double longitude = center.longitude + (radiusLongitude * Math.cos(theta));

        result.add(new LatLng(latitude, longitude));
    }

    return result;
}

第三步和最后一步是使用这些方法来创建PolygonOptions

static PolygonOptions createPolygonWithCircle(Context context, LatLng center, int radius) {
    return new PolygonOptions()
        .fillColor(ContextCompat.getColor(context, R.color.grey_500_transparent))
        .addAll(createOuterBounds())
        .addHole(createHole(center, radius))
        .strokeWidth(0);
}

我还创建了一个演示存储库,其中包含一个绘制所需圆形的应用程序。https://github.com/AntonyGolovin/Google-Map-mask。所有必要的逻辑都包含在MapHelper.java类中。

屏幕截图


1
非常出色,感谢。值得注意的是,在 createPolygonWithCircle 中半径单位为公里,并且很容易将半径值从整型转换为双精度浮点型。 - iaindownie

-1
我使用了这种方法从用户位置绘制了一个5000米半径的圆,但我不知道如何在您的演示图片中获得那种漂亮的高亮效果,这种方法会给您一个浅蓝色背景和深蓝色描边的圆。希望能有所帮助!
private void drawCircle(LatLng point){

        // Instantiating CircleOptions to draw a circle around the marker
        CircleOptions circleOptions = new CircleOptions();

        // Specifying the center of the circle
        circleOptions.center(point);

        // Radius of the circle
        circleOptions.radius(5000);

        // Border color of the circle
        circleOptions.strokeColor(0xFF0000FF);

        // Fill color of the circle
        circleOptions.fillColor(0x110000FF);

        // Border width of the circle
        circleOptions.strokeWidth(2);

        // Adding the circle to the GoogleMap
        mMap.addCircle(circleOptions);

    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接