3

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?

xbakesx
  • 13,202
  • 6
  • 48
  • 76
  • I haven't played with overlapping markers. Does the order of drawing seem to line up with the order of the markers based on index, as you provide them from your `ItemizedOverlay`? – CommonsWare Dec 14 '12 at 21:50
  • No, I should have mentioned that was my frist attempt. They seem to always draw from North to South so that it maintains that 3D look... – xbakesx Dec 14 '12 at 21:55
  • I guess I should have searched SO a little better: http://stackoverflow.com/questions/11182343/android-mapview-control-ordering-of-multiple-types-of-overlayitems – xbakesx Dec 14 '12 at 21:57
  • Hello xbakesx, I need to add custom infowindow on map using mapforge and osmdroid like you, can you please help me how to customize info window. Thanks – nitesh Apr 15 '15 at 13:17
  • @nitesh I don't have any experience with either of those, but if you want to write up a question with what you have so far, what you're trying to do and what you've have tried that doesn't seem to be working I'll sure give it a look! – xbakesx Apr 16 '15 at 14:43

2 Answers2

1

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.

Luis
  • 11,978
  • 3
  • 27
  • 35
  • This would be a much more elegant solution to the problem, but it doesn't seem to force the passed in item on top. The docs don't say that it does, but it also doesn't say it doesn't... https://developers.google.com/maps/documentation/android/v1/reference/com/google/android/maps/ItemizedOverlay#setFocus(Item) – xbakesx Dec 15 '12 at 04:31
  • I know docs dosen't refer it, because I've checked it before post. However, I've used it in one app and it does :-). I think you also need to call `mapView.invalidate()` after set the focus. – Luis Dec 15 '12 at 11:48
  • Just one more thing... I used a `StateListDrawable` but don't believe it makes any difference. The setFocus is just replicating the behaviour of tapping the marker, which brings it to the front. – Luis Dec 15 '12 at 11:56
  • I wouldn't think the drawable makes a difference. If I `mapView.invalidate()` it works if I wait until after the map has animated the marker to the center, then long press it the marker again, it sometimes pops up to the front. The animating definitely seems to remove focus from the item, not sure why it only seems to stick in the foreground after that sometimes... thoughts? – xbakesx Dec 17 '12 at 15:39
  • It looks reasonable to have the marker loosing the focus in sequence of an animation, otherwise the focous could rest on a marker the would be out of visible screen (in reality would be a non existing marker) ... Did you try to set the focus after you start the animation? If that dosen't solve it, you could try to post the `setFocous()` with a delay of a couple of secounds after start the animation. That would not be perfect but should do the trick. – Luis Dec 17 '12 at 22:05
  • I was setting the focus when the animation completed (as well as the `invalidate()` but that didn't change anything. – xbakesx Dec 17 '12 at 22:10
  • As I was not sure if you used a post delayed for the `setFocus()`, I updated my answer with an example how to do it, with a delay of half a second. Although I can't test it now, I'm quite sure that this should even with an animation. Let me know if not, and I'll come back to you. – Luis Dec 18 '12 at 00:45
  • I put the `setFocus` in a `postDelayed`, I tried it in a `postDelayed` after the animation completes. It's just not drawing the focused item last for me. I will make a brand new app that just has a `MapView` and `ItemizedOverlay` and see what happens. – xbakesx Dec 18 '12 at 14:10
0

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.

Community
  • 1
  • 1
xbakesx
  • 13,202
  • 6
  • 48
  • 76