1

I'm currently making an android map based app. The purpose of this app is to display complex overlays on a mapview. To do this I'm using custom overlays and overriding draw() function.

That part is working perfectly and now I'd like to handle onTap events for each overlay. What I'm currently doing:

public class MapOverlay extends Overlay {


    public MapOverlay(Kml kml,String color,Projection projection){
    }   

    public void draw(Canvas canvas, MapView mapv, boolean shadow){
        super.draw(canvas, mapv, shadow);

        // draw path...
    }

    @Override
    public boolean onTap(GeoPoint gp, MapView mapView) {
        Log.i("Overlay","Say whaaaat!");
        return super.onTap(gp, mapView);
    }
}

As expected, each overlay is covering the entire mapview, so when I tap on an overlay, every overlays fire the onTap event. Say I have 6 overlay, I can't tell which path has been tapped on.

Is there a solution to do that?

UPDATE:

Now I've succeeded displaying markers on my map using ItemizedOverlay (tied ItemizedBaloonOverlay as well) but it's not exactly what I want.

I'd like something like a PolygonOverlay that will detect if the tap was done inside the polygon and only fire a tap event in that case.

Thanks

Romain Piel
  • 11,017
  • 15
  • 71
  • 106

2 Answers2

0

Use the following method instead of onTap(GeoPoint gp,MapView map)

@Override
protected boolean onTap(int index) {
final OverlayItem item = mOverlays.get(index);        
//your code for your requirement
return true;
}
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • Thanks for your solution. onTap() is not available for Overlay base classe. Your solution is equivalent to Anand's one. In this case, do I need to draw my path on the overlay item or the itemizedoverlay itself? – Romain Piel Apr 06 '12 at 10:13
  • ya you must extend ItemizedOverlay. i mean it only for the classes which extends ItemizedOverlay. – Shankar Agarwal Apr 06 '12 at 10:15
  • Sorry but ItemizedOverlay is not adapted to my requirements. For more details, see my updated question. – Romain Piel Apr 06 '12 at 11:50
0

Based on this answer from another post, I have built my custom overlay and it works! So basically, no need of ItemizedOverlay stuff (except if you want to display an overlayitem).

Just take my previous MapOverlay class and add the basic onTap() function:

public class MapOverlay extends Overlay {

    private Path path;

    public MapOverlay(){
        super();
    }

    @Override
    public void draw(Canvas canvas, MapView mapv, boolean shadow) {
        super.draw(canvas, mapv, shadow);

            // draw your path and store it in variable path
    }

    @Override
    public boolean onTap(GeoPoint geoPoint, MapView mapView) {

        RectF rectF = new RectF();
        path.computeBounds(rectF, true);
        Region region = new Region();
        region.setPath(path, new Region((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom));

        Point point = new Point();
        mapView.getProjection().toPixels(geoPoint, point);

        if (region.contains(point.x, point.y)) {
            Log.d("onTap", point.x+" "+point.y);
        }

        return super.onTap(geoPoint, mapView);
    }

}

And that's it! The only thing left is to check the z-order if you only want to fire the one upside. Shouldn't be a big deal if I keep track of it in my MapOverlay class.

Community
  • 1
  • 1
Romain Piel
  • 11,017
  • 15
  • 71
  • 106