Android Overlay Entire Map
How would you go about creating and ItemizedOverlay which covers the entire map?
I have multiple markers on the map which are ItemizedOverlay classes.
I want to create another ItemizedOverlay which covers the entire map to intercept touch events which are not on my markers but the map itself. I have read in other SO questions like this one that the only way to do this is via an ItemizedOverlay->onTap. The problem is I don't know how to create of Drawable marker which covers the entire map.
I have experimented with LinearLayout drawables but it only appears to be able to use a drawable image as a marker.
Here is my code
private class BackgroundOverlay<Item extends OverlayItem> extends ItemizedOverlay<OverlayItem>{
private ArrayList<OverlayItem> overlays = new ArrayList<OverlayItem>();
/**
* @param defaultMarker
*/
public BackgroundOverlay(Drawable defaultMarker,Context context) {
super(boundCenterBottom(defaultMarker));
}
@Override
protected OverlayItem createItem(int i) {
return overlays.get(i);
}
@Override
public int size() {
return overlays.size();
}
public void addOverlay(OverlayItem overlay) {
overlays.add(overlay);
populate();
}
protected boolean onTap(int index){
Toast.makeText(getApplicationContext(), "Touched background",Toast.LENGTH_SHORT).show开发者_运维技巧();
return true;
}
}
and to create the overlay
Drawable d=this.getResources().getDrawable(R.layout.full_screen);
BackgroundOverlay<OverlayItem> lay = new BackgroundOverlay<OverlayItem>(d,this);
overlayItem = new OverlayItem(this.mapView.getMapCenter(), "", "");
lay.addOverlay(overlayItem);
mapOverlays.add(lay);
and the drawable xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/blue">
</LinearLayout>
Thanks in advance
Ok so I figured it out. To overlay the entire map you add a simple Overlay subclass with no drawable duh. and use the onTap event.
private class BackgroundOverlay extends Overlay{
public boolean onTap (final GeoPoint p, final MapView mapView){
Toast.makeText(getApplicationContext(), "Touched background",Toast.LENGTH_SHORT).show();
hideOtherFires(mapView.getOverlays());
return true;
}
and instantiate before the other overlays so it goes underneath
//add a background overlay to intercept touches on background
BackgroundOverlay lay = new BackgroundOverlay();
mapOverlays.add(lay);
You could just override Overlay's onTap method in one of your existing ItemizedOverlays:
/* (non-Javadoc)
* @see com.google.android.maps.Overlay#onTap(com.google.android.maps.GeoPoint, com.google.android.maps.MapView)
*/
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
super.onTap(p, mapView);
OverlayItem item = getFocus();
mapView.refreshDrawableState();
return false;
}
Make sure you pass it to the superclass, otherwise your onTap(int index) won't receive the event.
To put it differently:
onTap(int index)
- detects taps on OverlayItems
onTap(GeoPoint p, MapView mapView)
- detects taps anywhere on the map
精彩评论