Search code examples
androidandroid-mapviewitemizedoverlayoverlayitem

How do you control the order of OverlayItems on an ItemizedOverlay?


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?


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.