问题
I have searched for a while on this problem but have come up trumps.
I have a simple mapview that makes use of custom overlay items on a ManagedOverlay, let's call them redOverlayItem
and blueOverlayItem
. All the ManagedOverlay does is query my database for a set of locations of varying types and then place them on a map asynchronously.
My managed overlay works fine, however I can't seem to be able to control the "z" ordering. For instance, sometimes I have redOverlayItem
's covering blueOverlayItem
's and vice versa, where I really just want redOverlayItem
's to be on top.
I've exhausted my ideas on how to fix this. How can I control ordering of multiple types of overlay items?
回答1:
Alright guys, I figured it out.
In your Overlay, you need to override getIndexToDraw
//Overriding to return add order rather than latitude order.
protected int getIndexToDraw(int drawingOrder) {
return drawingOrder;
}
Rather than call super.getIndexToDraw(int), which returns items ordered by latitude value, all I'm doing now is returning the order in which they are added. Then all I do for my array of overlays is sort them accordingly (I'm using Rob Carmick's BeanComparator, but you could use any sorting function really)
BeanComparator bc = new BeanComparator(CustomOverlayItem.class, "getIsRed");
Collections.sort(overlayItemsArray, bc);
来源:https://stackoverflow.com/questions/11182343/android-mapview-control-ordering-of-multiple-types-of-overlayitems