android谷歌地图多边形添加圆孔

sn0*_*0ep 7 android google-maps

嗨,目前我在谷歌地图应用程序上工作.

我想要以下效果:

在此输入图像描述

要做到这一点,我首先在全国范围内创建一个多边形覆盖,然后为具有特定KM半径的突出显示区域的此多边形添加一个孔,以便在缩放时缩小和扩展.

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

mMap.addPolygon(new PolygonOptions().addAll(sCountryBorder).fillColor(0xcc000000));
Run Code Online (Sandbox Code Playgroud)

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

mMap.addPolygon(new PolygonOptions().addAll(sCountryBorder).fillColor(0xcc000000).addHole({CIRCULAR_HOLE}));
Run Code Online (Sandbox Code Playgroud)

我知道可以在谷歌地图中创建一个具有一定半径的圆圈是否也可以某种方式将其转换为LatLng对象数组?

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

Ant*_*vin 11

不幸的是,谷歌地图库不允许获得圆圈的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));
    }};
}
Run Code Online (Sandbox Code Playgroud)

第2步是创建一个方法,该方法将返回Iterable带有LatLng圆的s.

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;
}
Run Code Online (Sandbox Code Playgroud)

3,最后一步是使用这些方法进行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);
}
Run Code Online (Sandbox Code Playgroud)

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

结果:

截图


Ram*_*hDR -1

我用这个方法画了一个距用户位置5000m半径的圆,但我不知道如何在你的演示图片中获得漂亮的突出显示效果,这个方法给你一个浅蓝色背景和深蓝色描边的圆。希望能帮助到你!

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

    }
Run Code Online (Sandbox Code Playgroud)