Search code examples
androidpolygonosmdroid

osmdroid Polygon - adding a new point in a triangle


I can make a polygon using the singleTapConfirmedHelper(). But I still have a problem. How do I remove the line(blue arrow) when adding a fourth point(red circle). The line does not disappear after I add the fourth point. I hope I explained it well. Thank you.

    @Override
    public boolean singleTapConfirmedHelper(GeoPoint p) {

    Polygon circle = new Polygon();
    circle.setPoints(Polygon.pointsAsCircle(p, 2.0));
    circle.setFillColor(0x12121212);
    circle.setStrokeColor(Color.RED);
    circle.setStrokeWidth(2);
    map.getOverlays().add(circle);
    circle.setInfoWindow(new 
    BasicInfoWindow(org.osmdroid.bonuspack.R.layout.bonuspack_bubble, 
    map));
    circle.setTitle("Centered on " + p.getLatitude() + "," + 
    p.getLongitude());

    List<GeoPoint> pts = new ArrayList<>();
    pts.add(new GeoPoint(p.getLatitude(), p.getLongitude()));

    Polygon polygon = new Polygon(ctx);
    polygon.setTitle("This is a polygon");
    polygon.setSubDescription(Polygon.class.getCanonicalName());
    polygon.setFillColor(0x12121212);
    polygon.setVisible(true);
    polygon.setStrokeColor(Color.BLACK);
    polygon.setStrokeWidth(4);
    polygon.setInfoWindow(new 
    BasicInfoWindow(R.layout.bonuspack_bubble, map));
    polygon.setPoints(pts);
    map.getOverlays().add(polygon);

    map.invalidate();

    return true;
}

screenshot


Solution

  • You are drawing new polygon for each point added and you are not removing old ones. So theMapView will draw them all one over another. You should remove previously drawn polygon from list of overlays before calling map.invalidate().

    Polygon polygon = new Polygon(ctx);
    polygon.setTitle("This is a polygon");
    polygon.setSubDescription(Polygon.class.getCanonicalName());
    polygon.setFillColor(0x12121212);
    polygon.setVisible(true);
    polygon.setStrokeColor(Color.BLACK);
    polygon.setStrokeWidth(4);
    polygon.setInfoWindow(new 
    BasicInfoWindow(R.layout.bonuspack_bubble, map));
    polygon.setPoints(pts);
    map.getOverlays().add(polygon);
    if (oldPolygon != null) {
       map.getOverlays().remove(oldPolygon);
    }
    oldPolygon = polygon;
    
    map.invalidate();