Question

First of all I inherited a lot of code here, although I have combed through it a few times looking for a reason for this behavior and I am stumped.

I want the last tapped OverlayItem to be on top of the rest, even if it looks silly. What I am seeing is that while the MapView is animating (to center the OverlayItem) it does exactly what I want, then when it completes, the "selected one" jumps to the background again.

Is this the default behavior? Or is there something in my code that's janking this all up?

While animating:

enter image description here

Once centering animation is complete:

enter image description here

I can see a few ways of fixing this (drawing the selected OverlayItem myself in the draw() method or ensuring the selected is the last drawn), but what do people do in this situation? Or is this just a bug somewhere deep in my code I need to undo?

Was it helpful?

Solution

I believe that when you set the focus on a specific OverlayItem it's brougth to the front. Something like:

    myItemizedOverlay.setFocus(overlayItem);

With this, you don't need to play all the time with the items order.

--EDITED--

//Define this class level field
private Handler mHandler = new Handler();

//Use this after starting animation
mHandler.postDelayed(new Runnable() {

    @Override
    public void run() {
        myItemizedOverlay.setFocus(overlayItem);
        mapView.invalidate();  
    }
}, 500);

Regards.

OTHER TIPS

EDIT: It seems like ItemizedOverlay.setFocus(item) is the way to get a single item to be drawn on top (after a mapview.invalidate()). I can't get it to work the way I want it, because when the mapview animates it removes the focus and drawing order changes back.

Many thanks to Android Mapview: Control ordering of multiple types of OverlayItems? for pointing the drawing order behavior out to me.

For right now this is my heavy-handed solution. You can take complete control over the order in which the OverlayItems are shown by overriding getIndexToDraw in ItemizedOverlay.

protected int getIndexToDraw(final int drawingOrder)

The drawingOrder parameter is the same index as the parameter passed into createItem. You return the order in which the markers will be drawn (higher numbers are drawn later which means "on top").

In my particular case, I have an ArrayList of items that I sort and make sure the selected OverlayItem is always last in the list. Then my getIndexToDraw method looks like this:

@Override
protected int getIndexToDraw(final int drawingOrder)
{
    return drawingOrder;
}

In order to draw the OverlayItems in the same order as I have them stored in my ArrayList.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top