6

Is it possible to set an EventListener in a drawn Polygon in a osmdroid MapView Overlay ? I would like to print on a Overlay and after touching it I would like to change its color or handle data behind a Polygon.

For Example:

Poly1: ID = 1337, P1(0,0),P2(1,0),......,Pn(0,0)

Poly2: ID = 42 , P1(10,7),P2(18,39),......,Pn(10,7)

After touching in Poly1 I want to know aha ID 1337 is pressed. An want to change its color.

How could I implement such a behavior ?

Matthias H
  • 1,300
  • 13
  • 19
  • You can follow below link for Drawing Polyline on Map. https://stackoverflow.com/questions/39454857/how-to-buffer-a-polyline-in-android-or-draw-a-polygon-around-a-polyline/42664925#42664925 – Nikunj Peerbits Feb 19 '18 at 13:08

2 Answers2

0

Use this documentation to draw the polygons

Use this to listen for map clicks

Use this to determine if a touch is inside one of the polygons

I'm not sure that geometry library can run on android, so feel free to replace the third component.

EDIT:

Misread the question and associated it with google maps, sorry.

Machinarius
  • 3,637
  • 3
  • 30
  • 53
  • Sorry, im working with osmdroid, you come with the Google API. I have already drawn a Polygon, and I know how to get the position of an OnTouch event. Is the only way to determine if the touch was in a Polygon over using geometry libarys ? I hoped that the canvas maybe knows its drawings an can determine a klick on a drawing :( Not the case ? – Matthias H Jan 17 '13 at 10:55
  • Oh, sorry... overlooked that OSMDroid thingy. The point is that map libraries are just that, their scope does not include geometry directly/as a feature so yeah, you must provide it yourself somehow. Its still worth checking out the documentation for your map library though. – Machinarius Jan 17 '13 at 10:56
  • Ok i will take a look in JTS and use it to get the Polygon that is touched. So i think i will solve my problem :) Thanks – Matthias H Jan 17 '13 at 11:17
0

Here is how to draw and fill a polygon (rectangle example):

ArrayList bgRectPoints = new ArrayList<>();

    GeoPoint pt1 = new GeoPoint(-15.953548, 126.036911);
    bgRectPoints.add(pt1);
    GeoPoint pt2 = pt1.destinationPoint(10000, 0);
    bgRectPoints.add(pt2);
    GeoPoint pt3 = pt2.destinationPoint(10000, 90);
    bgRectPoints.add(pt3);
    GeoPoint pt4 = pt3.destinationPoint(10000, 180);
    bgRectPoints.add(pt4);
    bgRectPoints.add(pt1);

    Polygon polygon = new Polygon();
    polygon.setPoints(bgRectPoints);
    polygon.setFillColor(Color.BLACK);

    mapView.getOverlays().add(polygon);

To receive touch/Tap initialize polygon like this:

    Polygon polygon = new Polygon(){
        @Override
        public boolean onSingleTapConfirmed(MotionEvent event, MapView mapView) {
            Toast.makeText(context, "Polygon clicked!",Toast.LENGTH_SHORT).show();
            return super.onSingleTapConfirmed(event, mapView);
        }
    };

More can be found here

M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69