Naveedumar
Naveedumar

Reputation: 400

Android Google maps how to draw complex polygon with fillColor

am trying to draw free hand shape on google map up to now i can successfully draw Polyline and polygon thanks to @Chintan Khetiya reply at draw free hand polygon in Google map V2 in Android and Draw a path on maps but when i try to draw polygon like below image then "polygonOptions.fillColor(color)" d't work i try to convert point into two polygon but its also d't work can any body suggest or like to share any hint or better solution to draw free hand shape with fill color using google maps polygon.

polygon with colid

here is how i draw polygon

fram_map.setOnTouchListener(new View.OnTouchListener() {     
   @Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        int x_co = Math.round(x);
        int y_co = Math.round(y);

        projection = mMap.getProjection();
        Point x_y_points = new Point(x_co, y_co);

        LatLng latLng = mMap.getProjection().fromScreenLocation(x_y_points);
        latitude = latLng.latitude;

        longitude = latLng.longitude;

        int eventaction = event.getAction();
        switch (eventaction) {
            case MotionEvent.ACTION_DOWN:
                // finger touches the screen
                val.add(new LatLng(latitude, longitude));

            case MotionEvent.ACTION_MOVE:
                // finger moves on the screen
                val.add(new LatLng(latitude, longitude));

            case MotionEvent.ACTION_UP:
                // finger leaves the screen
                Draw_Map();
                break;
        }

        if (Is_MAP_Moveable == true) {
            return true;

        } else {
            return false;
        }
    }
});

public void Draw_Map() {
    rectOptions = new PolygonOptions();
    rectOptions.addAll(val);
    rectOptions.strokeColor(Color.BLUE);
    rectOptions.strokeWidth(7);
    rectOptions.fillColor(Color.CYAN);
    polygon = mMap.addPolygon(rectOptions);
}

a similar feature is used in "zillow real estate android app"

Upvotes: 4

Views: 2500

Answers (1)

Marco Capoferri
Marco Capoferri

Reputation: 224

As polygon documentation ( https://developers.google.com/android/reference/com/google/android/gms/maps/model/Polygon ) shows in the provided example you should close the shape setting as last LatLng a copy of the first one.

So you should edit your listener adding a reference to the LatLng obtained when you perform the ACTION_DOWN and when you perform an ACTION_UP you must insert a new LatLng with the same coords of the previously referenced point.

Happy coding!

Upvotes: 1

Related Questions