I'm having some troubles with displaying ItemizedOverlay(s) on a map. Here I extracted a simplified example of what I need to do. Here's my Map class:
import java.util.Timer;
import java.util.TimerTask;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class Map extends MapActivity {
protected static final String TAG = "Map";
private int test = 0;
private TestItemized items = null;
private Timer timer = new Timer();
private MapView map = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.map = (MapView) this.findViewById(R.id.myMapView1);
this.map.setBuiltInZoomControls(true);
this.map.setSatellite(false);
this.map.setStreetView(true);
this.map.setClickable(true);
this.timer.scheduleAtFixedRate(new TimerTask(){
@Override
public void run() {
Log.d(TAG, "refeshing: "+ String.valueOf(test));
Drawable drawable = Map.this.getResources().getDrawable(R.drawable.androidmarker);
test++;
if (Map.this.items instanceof TestItemized && Map.this.map.getOverlays().contains(Map.this.items))
Map.this.map.getOverlays().remove(Map.this.items);
//some code might go here to retrieve new coordinates but this isn't a problem
Map.this.items = new TestItemized(drawable, Map.this);
double lat = 46.491734;
Log.d(TAG,"Latitude : "+ String.valueOf(lat));
double lng = 11.320365;
Log.d(TAG,"Longitude : "+ String.valueOf(lng));
OverlayItem item = new OverlayItem(new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6)), String.valueOf(Map.this.test), "blah!");
Map.this.items.addOverlay(item);
Map.this.map.getOverlays().add(Map.this.items);
Log.d(TAG,"refreshing "+String.valueOf(test)+" is over");
}}, 1000,60000);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
I followed the guide provided by google to extends the ItemizedOverlay class
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.Toast;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
@SuppressWarnings("rawtypes")
public class TestItemized extends ItemizedOverlay {
private Context mCtx;
private List<OverlayItem> mItems = new ArrayList<OverlayItem>();
public TestItemized(Drawable arg0) {
super(arg0);
}
/**
* @param defaultMarker
* @param mCtx
*/
public TestItemized(Drawable defaultMarker, Context mCtx) {
super(defaultMarker);
this.mCtx = mCtx;
}
public void addOverlay(OverlayItem overlay) {
this.mItems.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return this.mItems.get(i);
}
@Override
public int size() {
return mItems.size();
}
/* (non-Javadoc)
* @see com.google.android.maps.ItemizedOverlay#onTap(int)
*/
@Override
protected boolean onTap(int index) {
OverlayItem item = this.mItems.get(index);
Toast.makeText(this.mCtx, item.getTitle() +" : " +item.getSnippet(), Toast.LENGTH_LONG).show();
return true;
}
}
As it is clear from the code I need to update at fixed times a map UI with possibly new positions to do so I'm using a timer, but I think I might fall in some kind of thread issues I don't know how to handle, since when running a test I can see in the Log what happens but no marker is drawn on the map. Does anybody know how to solve this kind of problem?