I'm trying to iterate through a hashmap and and use the key during my iteration. I decided to use it inside an Async Task because the iteration was blocking my UI thread.
My hashmap has LatLng as the Key and a Marker as the Object, when I get the key during Iteration and pass it to a new LatLng that works fine but when I try to use this new LatLng I get a java.util.ConcurrentModificationException
exception. Strangely this doesn't happen when I use the code without the AsyncTask
Here is my code
class AddCityMarker extends AsyncTask<Integer, Integer, String> {
protected String doInBackground(Integer... counter) {
//CityMarker cMarker;
LatLng marker_loc;
List<String> city_markers = new ArrayList<String>();
if(displayed.size() > 0){
Iterator<HashMap.Entry<LatLng, Marker>> myIterator = displayed.entrySet().iterator();
while(myIterator.hasNext()) {
//cMarker = new CityMarker();
HashMap.Entry<LatLng, Marker> entry = myIterator.next();
marker_loc = entry.getKey();
Log.i("ZOOM", "Key = " + marker_loc + ", Value = " + entry.getValue());
List<Address> addresses = null;
try {
//This is where I get the error
addresses = gcode.getFromLocation(marker_loc.latitude, marker_loc.longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
if(addresses.size() > 0) {
if(!city_markers.contains(addresses.get(0).getLocality())){
city_markers.add(addresses.get(0).getLocality());
//map.addMarker(new MarkerOptions().position(marker_loc).snippet("CITY"));
}
}
}
}
return null;
}
protected void onPostExecute(String jsonResult) {
try {
if(isClubMarkers){
map.clear();
isClubMarkers = false;
}
displayed.clear();
} catch (Exception e) {
e.printStackTrace();
}
}
}