Search code examples
androidmapsoverlaypoints

Can I alter the order that points in an Maps Overlay are drawn on the Map


I have an Maps Overlay which contains multiple points, each with its own numbered marker. The points are in the overlay in the order I would like them to be drawn (ie from Marker 0 to 10 - with 10 being the latest point). When the map is displayed it appears that points are drawn in the order of increasing Latitude. This means than in my case the marker for point 7 may be drawn on top of the marker for point 10. Is there any way of controlling the order in which the markers will be laid out on the map?


Solution

  • I found it - I have added the following code and it seems to work. This forces the items to be drawn in the same order as they are in the Overlay and not in order of increasing Latitude.

    Interestingly, if you choose a different order (e.g. reversed) then the map will display as you ask it with earlier entries overlaying later ones. i.e. point 7 will be drawn on top of point 10 if they both have the same map coordinates. However when you tap on a what is displayed as point 7, the onTap method will give you the index for point 10 (presumably because it was drawn 10th in the sequence) which may cause you some confusion. It is then up to you to remember the sequence you asked points to be displayed and work out which entry is actually on top.

        @Override
    protected int getIndexToDraw(int drawingOrder) {
        // Log.d("getIndexToDraw", "drawing order: " + drawingOrder);  //just my debug
        return drawingOrder; // forces order to follow overlay item sequence
        // return super.getIndexToDraw(drawingOrder); // default code - ie Latitude order
    }