1

I am a beginner when it comes to working with the Maps API so please bear with me and I know there have been many other posts dealing with the same issue but I am still stuck.

  • I have been able to place some overlay images onto my map. The only issue I am having now is that I do not know how to make them disappear when I zoom out enough levels as this causes the overlay images to crowd together and overlap - basicaly making them useless at that level. So, any help on how to make them appear (after zoom level 18) would be truly appreciated.

  • I have tried using a zoom listener and an if statement but it had no change - most likely because I do not know where exactly I need to implement it and/or what other methods are required to enable it. Also, I am not sure on how to implement the draw() method as many others have used this to make it scale and disappear.

Edit:

These are the two classes I have so far which execute successfully (after applying the answer):

The Map.java file:

public class Map extends com.google.android.maps.MapActivity implements
    OnOverlayGestureListener {

private boolean mShowOverlays = true;
private MapView mMapView;
MapView mapView;
MapController mapController;

private void setOverlayVisibility() {
    boolean showOverlays = mMapView.getZoomLevel() > 18;
    if (showOverlays != mShowOverlays) {
        mShowOverlays = showOverlays;
        for (Overlay overlay : mMapView.getOverlays()) {
            if (overlay instanceof ItemOverlay) {
                ((ItemOverlay) overlay).setVisible(showOverlays);
            }
        }
    }

}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapController = mapView.getController();
    mapController.setZoom(17);

    boolean showOverlays = mMapView.getZoomLevel() > 18;

    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable lot = this.getResources().getDrawable(R.drawable.lot);
    ItemOverlay parking_lot = new ItemOverlay(lot, this);
    GeoPoint point1 = new GeoPoint(43806622, -79219797);
    OverlayItem parking = new OverlayItem(point1, "Shopping Center","Parking Lot");

    parking_lot.addOverlayItems(parking);
    mapOverlays.add(parking_lot);

    Drawable logo = this.getResources().getDrawable(R.drawable.entrance);
    ItemOverlay ent = new ItemOverlay(logo, this);
    GeoPoint start = new GeoPoint(43805697, -79221031);
    mapController.setCenter(start);
    OverlayItem welcome = new OverlayItem(start, "Welcome", " ");

    ent.addOverlayItems(welcome);
    mapOverlays.add(ent);

    public <ZoomEvent> boolean onZoom(ZoomEvent ze, ManagedOverlay mo) {
        setOverlayVisibility();    
        return true;
    }

}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}

The ItemOverlay.java file:

public class ItemOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
private boolean visible = true;

private boolean mVisible = true;

    public void setVisible(boolean value) {
       mVisible = value;
    }
    public boolean isVisible() {
       return mVisible ;
    }

    @Override
    public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow) {
        if (mVisible) {
            super.draw(canvas, mapView, shadow);
        }
     }

public ItemOverlay(Drawable defaultMarker, Context context) {
    super(boundCenterBottom(defaultMarker));
    // TODO Auto-generated constructor stub
    mContext = context;
}

public void addOverlayItems(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}

@Override
protected OverlayItem createItem(int i) {
    // TODO Auto-generated method stub
    return mOverlays.get(i);

}

@Override
public int size() {
    // TODO Auto-generated method stub
    return mOverlays.size();
}

@Override
protected boolean onTap(int index) {
    // TODO Auto-generated method stub
    OverlayItem item = mOverlays.get(index);
    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
    dialog.setTitle(item.getTitle());
    dialog.setMessage(item.getSnippet());
    dialog.show();
    return true;
}

}
Nate
  • 31,017
  • 13
  • 83
  • 207
TK_
  • 13
  • 4

1 Answers1

1

I really like using the OverlayManager library for Android. It adds features to the Google Maps code, and makes a few things a lot easier. Find it here including some demo code that uses it

Option #1: If you use this, then you can use the OverlayManager's gesture listener interface for your MapActivity, to receive a callback for each zoom (in/out) event.

public class Map extends MapActivity implements OnOverlayGestureListener 
{ 
    private boolean mShowOverlays = true;
    private MapView mMapView;     // assign this in onCreate()

    private void setOverlayVisibility() {
        boolean showOverlays = mMapView.getZoomLevel() >= 18;
        if (showOverlays != mShowOverlays) {
           mShowOverlays = showOverlays;
           for (Overlay overlay : mMapView.getOverlays()) { 
              if (overlay instanceof ItemOverlay) { 
                 ((ItemOverlay)overlay).setVisible(showOverlays); 
              } 
           } 
        }
    }

    // this is the onOverlayGestureListener callback:
    public boolean onZoom(ZoomEvent ze, ManagedOverlay mo) {
        setOverlayVisibility();    
        return true;
    }
}

You will have to also add your Map instance as a gesture listener with ManagedOverlay.setOnOverlayGestureListener(). See the sample code for that.

Finally, in your ItemOverlay class, you can override the draw() method, and selectively draw based on whether the overlay has been marked as visible or not. You need to add a custom visible property:

public class ItemOverlay extends ItemizedOverlay {

    private boolean mVisible = true;

    public void setVisible(boolean value) {
       mVisible = value;
    }
    public boolean isVisible() {
       return mVisible ;
    }

    @Override
    public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow) {
       if (mVisible) {
           super.draw(canvas, mapView, shadow);
       }
    }
 }

Option #2: Now, using the Overlay Manager library just for this one purpose might be overkill. So, another, probably simpler alternative is to create a zoom listener in the way described in this stack overflow answer. The code Kurru provides would go in your Map class. You would replace this in the answer's code:

checkMapIcons();

with the method I showed above:

setOverlayVisibility();

So, now you have two ways to "watch" the zoom level, and overriding ItemOverlay.draw() allows you to make the markers disappear whenever you like (zoom level < 18 in this example).

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • So, first of all, thank you for the fast responses and comments. Now, when I added the suggested code, it is clear that it requires the other classes from the OverlayManagerdemo so before I go any further, should I create and copy ALL of the classes used in the demo because at the moment, I see that ZoomEvent is not defined in the code as it is.. – TK_ Jul 05 '12 at 05:49
  • So, after seeing your option 2, I saw that it makes a lot more sense and is easier to apply. Once I had all the code suggested, everything seemed fine but when I ran the emulator, it started the map but then "stopped unexpectedly" immediately. I am not quite sure where it is causing a conflict... – TK_ Jul 05 '12 at 17:21
  • I got it to work with Option 2 - its awesome! I just had to change one of my variables in order to accept the correct input parameters (from mMapView to mapView). It works just how I wanted to - Thanks so much Nate. I really appreciate taking the time to help starters like me out. – TK_ Jul 05 '12 at 17:49
  • @TK_, you're welcome. On stack overflow, if you get a solution that solves the problem, they like you to mark it as solved by checking the little checkmark that shows up next to the answer that solved it for you. After you gain a few points, you can also vote on answers with the up/down arrows next to each answer. You can actually vote on multiple answers, but can only **Accept** one. – Nate Jul 05 '12 at 21:16