Skip to content Skip to sidebar Skip to footer

How To Get Ontap In Polygon In Osmdroid?

I have lot's of polygon drawn. Some of which overlaps. I want to be able to detect a tap inside the polygon and display a dialog saying it was clicked inside the polygon (this poly

Solution 1:

There are various answers, depending on what you want to achieve exactly.

Simple case: if you just want to have a bubble opening, then just set an InfoWindow to your Polygon, as described in the tutorial.

If you want anything else: for Polygons, there is no Listener available, as there are for Markers and Polylines (an enhancement to request, maybe?).

So what you can do is sub-class Polygon - as you did with your MyPolygon. Then override onTap method - or most likely onSingleTapConfirmed - and implement the behaviour you need.

Looking at Polygon.onSingleTapConfirmed source may help (hit test for instance).

Solution 2:

This is how I'm using it:

classCustomTapPolygonextendsPolygon {

    privateCustomObject tag;

    publicCustomObjectgetTag() {
        return tag;
    }

    publicvoidsetTag(CustomObject tag) {
        this.tag = tag;
    }

    @OverridepublicbooleanonSingleTapUp(MotionEvent e, MapView mapView) {
        if (e.getAction() == MotionEvent.ACTION_UP && contains(e)) {
             // YOUR CODE HEREreturntrue;
        }
        returnsuper.onSingleTapUp(e, mapView);
    }
}

Solution 3:

Have you tried osmbonuspack yet? I know for certain that there's an onclick listener for polygons in it.

Post a Comment for "How To Get Ontap In Polygon In Osmdroid?"