0

I need to check if the data of a newly created OverlayItem is already existing on the list of OverlayItems already displayed on the map. I made a code to check if the data of the OverlayItem is already existing but I'm encountering an error on it. How can I extract an OverlayItem from an Overlay?

My current code is like this:

//where mapOverlays = mapView.getOverlays() and overlayItem is the newly created overlayItem
public boolean isExisting(List<Overlays> mapOverlays, OverlayItem overlayItem)
{
    ItemizedOverlay overlay;
    OverlayItem itemToCompare;

    for(int i = 0; i < mapOverlays.size(); i++)
    {
        overlay = (ItemizedOverlay)mapOverlays; //I am getting an error here: java.util.Collections$SynchronizedRandomAccessList (from e.getMessage()). The stack trace does not contain any specific exception but only the trace of the error pointing to this line.
        existingOverlayItem = overlay.getItem(i);

        if(itemToCompare.getPoint().equals(overlayItem.getPoint())
            && itemToCompare.getSnippet().equals(overlayItem.getSnippet())
            && itemToCompare.getTitle().equals(overlayItem.getTitle()))
            return true; //if all data are the same
    }

    return false;
}
Arci
  • 6,647
  • 20
  • 70
  • 98
  • 1
    how have you created your class `MapOverlay` is it a List class?? it doesn't seem to be compatible with the `List`. i would also change you loop to the foreach one. `for(Overlay ol: mapOverlays){.....}`. what is `itemToCompare`? show more info to understand it. – Sergey Benner Feb 10 '12 at 10:01
  • Yes, I created MapOverlay. It extends ItemizedOverlay. itemToCompare is same as existingOverlayItem. I already updated my code. – Arci Feb 10 '12 at 10:12
  • No. I'm still encountering the same problem. I changed MapOverlay to ItemizedOverlay. – Arci Feb 10 '12 at 10:33
  • 1
    `ItemizedOverlay` is not a List either. If you want to avoid having duplicate items you should use HashSet and read about overriding `equals()` and `hashCode()` for your pojo and comparing the object. – Sergey Benner Feb 10 '12 at 10:38

3 Answers3

0

i don't know MapOverlay, but you probably can"t put List into a MapOverlay

0

That code looks a bit weird. Where is coming itemToCompare? I can't see it declared anywhere. If itemToCompare is the same Class as overlayItem, why not override equals in the class and use it instead of comparing every parameter?

Even better, overriding equals in the Class will grant you access to:

mapOverlays.contains(overlayItem); 

So your entire isExisting method will be replaced by the call above. You can see more information on how equals work here.

Luis Ollero
  • 383
  • 1
  • 2
  • 8
0

Thanks all for answering my question. I found out that my problem happens because I'm casting mapOverlays to a wrong class.

Also, instead of extracting OverlayItem from mapOverlays (which I can't find out how), I am just making the validation inside addOverlay(OverlayItem overlayItem). I can't do this before because I am always reinitializing the value of my itemizedOverlay. As such, I can't compare the newly added OverlayItem to the old ones because itemizedOverlay does not hold the old values anymore. What I did is instead of always reinitializing itemizedOverlay, I am just always clearing mapOverlays which equals to mapView.getOverlays().clear().

Arci
  • 6,647
  • 20
  • 70
  • 98