Recently I have drew a custom polygon around my country by creating a overlay class and drew it using path.
I have many sector of a map, each sector is divided and filled with color with a overlay class. However when I use the ontap function, only the last overlay item ontap function get called.
I believe is because I did not set a boundary for the overlay? The following is my overlay code
public class SectorOverlay extends Overlay{
CustomPolygon customPolygon =null;
Context context;
public SectorOverlay(Context context, CustomPolygon customPolygon) {
this.context=context;
this.customPolygon =customPolygon;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
shadow=false;
Paint paint = new Paint();
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(2);
paint.setColor(0x10000000);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setAntiAlias(true);
Point point1_draw = new Point();
if(customPolygon!=null)
{
Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
for(int n=0;n<customPolygon.getCorrdinateList().size();n++)
{
GeoPoint sector1 = new GeoPoint((int)(customPolygon.getCorrdinateList().get(n).getLatitude()*1e6), (int)((customPolygon.getCorrdinateList().get(n).getLongitude())*1e6));
if(n==0){
mapView.getProjection().toPixels(sector1, point1_draw);
path.moveTo(point1_draw.x,point1_draw.y);
}else
{
mapView.getProjection().toPixels(sector1, point1_draw);
path.lineTo(point1_draw.x,point1_draw.y);
}
}
path.close();
canvas.drawPath(path, paint);
}
}
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
new CommonOpearation().showToast(context, customPolygon.getName());
return true;
}
}
It is being called for the “last”
Overlay, because it is the topmost.MapViewdraws its overlays from0..end, but when there is an event, the last drawn is on top, so it gets the event first, and since youreturn trueinOverlay.onTap(same applies toOverlay.onTouchEvent, and practically a lot of Android events) you’re saying that the event is handled, therefore it doesn’t bother calling the overlays down the line. So the event handlers are called inend..0order.I did not use
onTap, but according to CommonsWare at Android – Map overlay onTouchEvent / onTap howto? if you useItemizedOverlayyou should getonTaponly for your bounded/drawn area.It is surely possible that common
onTapandonTouchEventsare called for any touch point on screen. In this case you can find theGeoPointwithgetProjectionfromMapViewapplying it reversly to (x,y) getting (lon,lat). Or usingonTap(again, I haven’t heard about this before).If you need to do a hit-test with the drawn polygon of the overlay, here’s one which helps zou determine whether the point is in the (not necessarily convex) polygon:
http://verkkoopetus.cs.utu.fi/vhanke/trakla/PointInPolygon.html
LStart.x = minXOfPolygon, LEnd.x = maxXOfPolygon, LStart.y = LEnd.y = 0(you may need extend the line by 1 unit at
LStart.xandLEnd.xjust to be safe.)findIntersectionfrom http://workshop.evolutionzone.com/2007/09/10/code-2d-line-intersection/