我在地图上显示 ItemizedOverlay(s) 时遇到了一些麻烦。在这里,我提取了一个我需要做的简化示例。这是我的地图类:
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;
}
}
我按照谷歌提供的指南扩展了 ItemizedOverlay 类
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;
}
}
从代码中可以清楚地看出,我需要在固定时间更新可能有新位置的地图 UI,所以我正在使用计时器,但我认为我可能会遇到某种线程问题,我不知道该怎么做句柄,因为在运行测试时,我可以在日志中看到发生了什么,但地图上没有绘制标记。有谁知道如何解决这种问题?