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 polygon should be detected on tap). This kind of property can be seen with ItemizedIconOverlay where we can detect all of the OverlayItem with the tap on the OverlayItem. How to implement this on polygon?
The way I draw and add Polygon is:
MyPolygon myNewPolygon = new MyPolygon(this);
myPolygon.setPoints(Polygon.pointsAsCircle(new GeoPoint(38.948714, -76.831918), 20000.0));
map.getOverlays().add(myPolygon);
myNewPolygon.setPoints(Polygon.pointsAsCircle(new GeoPoint(38.851777, -77.037878), 20000.0));
map.getOverlays().add(myNewPolygon);
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).
Have you tried osmbonuspack yet? I know for certain that there's an onclick listener for polygons in it.
This is how I'm using it:
class CustomTapPolygon extends Polygon {
private CustomObject tag;
public CustomObject getTag() {
return tag;
}
public void setTag(CustomObject tag) {
this.tag = tag;
}
@Override
public boolean onSingleTapUp(MotionEvent e, MapView mapView) {
if (e.getAction() == MotionEvent.ACTION_UP && contains(e)) {
// YOUR CODE HERE
return true;
}
return super.onSingleTapUp(e, mapView);
}
}