Android Google Maps Polygon添加圆孔 [英] android google maps Polygon add circle hole

查看:51
本文介绍了Android Google Maps Polygon添加圆孔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您当前正在使用Google Maps应用程序.

hi currently im working on a google maps app.

我想要以下效果:

要做到这一点,我首先要在全国范围内创建多边形叠加层,然后在该多边形中为具有一定KM半径的高亮区域添加一个孔,以便在缩放时缩小和扩展.

To do this i was thing of first creating a polygon overlay over the country, following by adding a hole to this polygon for the highlighted area with a certain KM radius, so that it shrinks and expands when zooming.

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

Now i know how to create a polygon;

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

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

Now i want to add a hole to this polygon, but i dont know how to generate a circular hole with the correct radius.

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

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

I know it is possible to create a circle with a certain radius in Google maps is it also possible to somehow convert this into an Array of LatLng objects?

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

推荐答案

不幸的是,Google Maps库不允许获取圆形的LatLng数组.因此,您需要自己画一个圆.

Unfortunately, Google Maps library doesn't let to get an array of LatLng of the circle. So you need to draw a circle yourself.

基本上,您需要提供一种方法,该方法将为填充地图的多边形创建孔(圆).

Basically, you need to provide a method that will create a hole (circle) for a polygon which fills your map.

共有3个步骤.

第一步是建立一种方法,该方法将创建覆盖整个地图的多边形.

1 step is to build a method that will create a polygon that covers an entire map.

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

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

2 step is to create a method that will return an Iterable with LatLngs of the circle.

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

3,最后一步是使用这些方法创建PolygonOptions.

3 and the last step is to use these methods for PolygonOptions creation.

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 类.

I have also created a demo repository which contains an app which draws a needed circle. https://github.com/AntonyGolovin/Google-Map-mask. All needed logic is contained in MapHelper.java class.

结果:

这篇关于Android Google Maps Polygon添加圆孔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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