Domanda

sto avendo alcuni problemi con la visualizzazione ItemizedOverlay (s) su una mappa. Qui ho estratto un esempio semplificato di quello che devo fare. Ecco la mia classe di mappa:

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;
}
}

Ho seguito la guida fornita da Google per estende la classe 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;

}


} 

Come risulta dal codice ho bisogno di aggiornamento ad orari prestabiliti un'interfaccia utente con mappa possibilmente nuove posizioni per fare in modo che sto utilizzando un timer, ma penso che potrei cadere in una sorta di problemi di thread non lo faccio sapere come gestire, dal momento che durante l'esecuzione di un test che posso vedere nel registro ciò che accade, ma non segnalino viene disegnato sulla mappa. Qualcuno sa come risolvere questo tipo di problema?

È stato utile?

Soluzione

Al termine del metodo run (), dopo aver terminato l'aggiornamento MapView si dovrebbe chiamare

Map.this.map.postInvalidate();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top