Pregunta

Estoy teniendo algunos problemas con mostrar ItemizedOverlay (s) en un mapa. Aquí extraje un ejemplo simplificado de lo que tengo que hacer. Aquí está mi clase de mapa:

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 seguido la guía proporcionada por Google a extiende la clase 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;

}


} 

Como es evidente a partir del código es necesario que actualice a horas fijas una interfaz de usuario mapa con, posiblemente, nuevas posiciones para hacer así que estoy usando un contador de tiempo, pero creo que podría caer en algún tipo de problemas de rosca no lo hago saber cómo manejar, ya que cuando se ejecuta una prueba de que puedo ver en el registro de lo que sucede, pero ningún marcador se dibuja en el mapa. ¿Alguien sabe cómo solucionar este tipo de problemas?

¿Fue útil?

Solución

Al final del método run (), después de que termine la actualización de su MapView debe llamar

Map.this.map.postInvalidate();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top