Issue
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
Solution
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.
Answered By - Romain Piel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.