How to draw a Polygon on MapView, fill it, and put a onTouch Event on it
Asked Answered
P

2

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 ?

Postrider answered 17/1, 2013 at 10:23 Comment(1)
You can follow below link for Drawing Polyline on Map. #39455357Roush
N
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.

Nomarchy answered 17/1, 2013 at 10:29 Comment(3)
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 ?Postrider
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.Nomarchy
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 :) ThanksPostrider
V
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

Vulgarity answered 13/2, 2018 at 10:45 Comment(1)
Polygon.setFillColor() is now deprecatedEsqueda

© 2022 - 2024 — McMap. All rights reserved.