Issue
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;
}
}
Solution
It is being called for the "last" Overlay
, because it is the topmost. MapView
draws its overlays from 0..end
, but when there is an event, the last drawn is on top, so it gets the event first, and since you return true
in Overlay.onTap
(same applies to Overlay.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 in end..0
order.
I did not use onTap
, but according to CommonsWare at Android - Map overlay onTouchEvent / onTap howto? if you use ItemizedOverlay
you should get onTap
only for your bounded/drawn area.
It is surely possible that common onTap
and onTouchEvents
are called for any touch point on screen. In this case you can find the GeoPoint
with getProjection
from MapView
applying it reversly to (x,y) getting (lon,lat). Or using onTap
(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
- for 2.
LStart.x = minXOfPolygon, LEnd.x = maxXOfPolygon, LStart.y = LEnd.y = 0
(you may need extend the line by 1 unit atLStart.x
andLEnd.x
just to be safe.) - for 4. use
findIntersection
from http://workshop.evolutionzone.com/2007/09/10/code-2d-line-intersection/
Answered By - TWiStErRob
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.