我正在尝试动态添加标记(不是静态)。动态意义,当我从服务器获得更新时添加标记。
所以,以下是我的ItemizedOverlay代码:
private class VehicleItemizedOverlay extends ItemizedOverlay<OverlayItem>{
private ArrayList<OverlayItem> mOverlayItems = new ArrayList<OverlayItem>();
public VehicleItemizedOverlay(Drawable pDefaultMarker, ResourceProxy pResourceProxy) {
super(pDefaultMarker, pResourceProxy);
}
@Override
public boolean onSnapToItem(int arg0, int arg1, Point arg2, IMapView arg3) {
return false;
}
@Override
protected OverlayItem createItem(int pos) {
return mOverlayItems.get(pos);
}
@Override
public int size() {
if(mOverlayItems!=null) return mOverlayItems.size();
else return 0;
}
public void addOverlayItem(double longitude, double latitude, Bitmap bitmap){
if(Preferences.DEBUG) Log.d("MapActivity", "Latitude: "+latitude+" Longitude: "+longitude);
MarkerDrawable drawable = new MarkerDrawable(MapActivity.this, bitmap);
OverlayItem item = new OverlayItem("a", "a", new GeoPoint((int)(latitude*1e6), (int)(longitude*1e6)));
item.setMarker(drawable);
item.setMarkerHotspot(HotspotPlace.CENTER);
mOverlayItems.add(item);
populateNow();
}
public void populateNow(){
populate();
}
}
以下是方式,我正在创建ItemizedOverlay&amp;在我的活动onCreate()中将它添加到MapView:
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
mVehicleOverlay = new VehicleItemizedOverlay(getResources().getDrawable(R.drawable.stub), mResourceProxy);
mMapView.getOverlays().add(mOverlay);
现在,每当我收到服务器的回复时,我就会创建&amp;将overlayItems添加到ItemizedOverlay。
//onResponse is callback I received on receiving response. Vehicle is my vehicle object
public void onResponse(ArrayList<Vehicle> aVehicles) {
for(int i=0;i<mVehicles.size();i++){
final int lat = (int)(mVehicles.get(i).getLatitude()*1e6);
final int lon = (int)(mVehicles.get(i).getLongitude()*1e6);
mVehicleOverlay.addOverlayItem(lon, lat, bitmap);
}
}
但是所有叠加项目在地图的中心点处相互重叠。我错过了什么吗?
另外,如果我在添加静态覆盖项之前将其添加到mVehicleOverlay,那么它可以正常工作。
mVehicleOverlay = new VehicleItemizedOverlay(getResources().getDrawable(R.drawable.stub), mResourceProxy);
final int lat = (int)(STATIC_LATITUDE*1e6);
final int lat = (int)(STATIC_LONGITUDE*1e6);
mOverlay.addOverlayItem(lon, lat, bitmap);
mMapView.getOverlays().add(mVehicleOverlay);
在OSMDroid中,添加自定义列表覆盖后,我不应该添加&amp;像我们在Google API v1中一样填充overlayItems吗?或者我在这里遗漏了什么?