android google maps Polygon add circle hole

前端 未结 2 1626
情书的邮戳
情书的邮戳 2020-12-13 22:34

hi currently im working on a google maps app.

and i want the following effect:

To do this i was thing of first creating a polygon overlay over the c

相关标签:
2条回答
  • 2020-12-13 22:41

    I used this method to draw a circle with 5000m radius from user's location, but I do not know how to get that nice highlighted effect in your demo pic, this method gives you a circle with light blue background and a dark blue stroke. Hope it helps!

    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);
    
        }
    
    0 讨论(0)
  • 2020-12-13 22:44

    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.

    There are 3 steps.

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

    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.

    Result:

    0 讨论(0)
提交回复
热议问题