2010-11-18 86 views
0

我有這個問題,我認爲mimapmap項目的位置,但是當我移動或縮放時它們只會重新繪製地圖...我更新mimapmap項目的位置,但當我移動或縮放地圖時,它們只會重新繪製

我想,每次possitions得到了更新,那麼他們得到重新繪製在地圖上,而無需移動或用手指

我怎麼能做到這一點放大它的時間?

無效是沒有答案的,因爲它使所有的完全重繪,需要重新下載從互聯網地圖...

我的代碼:

public class Locate extends MapActivity{ 

private TextView userText = null; 
private TextView permissionText = null; 
private TextView lastUpdateText = null; 
private Button locateButton = null; 
private Button traceButton = null; 
private MapView mapView = null; 
List<Overlay> mapOverlays = null; 
double lat; 
double lng; 
GeoPoint p; 
MapController mc; 

Drawable drawable; 
Drawable drawable2; 
MyItemizedOverlay itemizedoverlay; 
MyItemizedOverlay itemizedoverlay2; 

//para almacenar la config local de mi app, mostrarme o no en el mapa... 
static SharedPreferences settings; 
static SharedPreferences.Editor configEditor; 

//private MyLooper mLooper; 

////////////////////////////////////////MANEJADOR SIMILAR A UN HILO, QUE ME PERMITE RECOGER MI POSICION ACTUAL Y ACTUALIZARLA EN EL MAPA UNA VEZ CADA 5 SEGUNDOS 
private Handler mHandler; 
private Runnable updateRunnable = new Runnable() { 
    @Override public void run() { 
     itemizedoverlay2.clear(); 
     updateMyPosition(); 
     queueRunnable(); 
    } 
}; 
private void queueRunnable() { 
    mHandler.postDelayed(updateRunnable, 5000); 
} 
/////////////////////////////////////////FIN MANEJADOR 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.locate); 

    userText = (TextView) findViewById(R.id.User); 
    permissionText= (TextView) findViewById(R.id.Permission); 
    lastUpdateText= (TextView) findViewById(R.id.LastUpdate); 
    locateButton = (Button) findViewById(R.id.locate); 
    traceButton = (Button) findViewById(R.id.trace); 
    mapView = (MapView) findViewById(R.id.mapview); 
    mapView.setBuiltInZoomControls(true); 
    mapOverlays = mapView.getOverlays(); 
    mc = mapView.getController(); 

    settings=PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()); 
    configEditor = settings.edit(); 

    drawable = this.getResources().getDrawable(R.drawable.minifriend); // Icono de la cara, para posiciones de mis amigos 
    drawable2 = this.getResources().getDrawable(R.drawable.miniicon2); // Icono del programa, para mi posicion GPS 
    itemizedoverlay = new MyItemizedOverlay(drawable, this); // Aqui almaceno otras posiciones gps 
    itemizedoverlay2 = new MyItemizedOverlay(drawable2, this); // Aqui almaceno mi posicion gps 

    if (this.getIntent().getExtras() != null) /// si he recibido datos del friend en el Bundle 
    { 
     updateFriendPosition(); 
    } 

    if (settings.getBoolean("showMeCheckBox", true)) 
    { 
     updateMyPosition(); 
    } 


    locateButton.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      // Perform action on clicks 
      //itemizedoverlay2.getItem(0). 
      itemizedoverlay.clear(); 
      updateFriendPosition(); 
      itemizedoverlay2.clear(); 
      updateMyPosition(); 
     } 
    }); 

    traceButton.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      // Perform action on clicks    
     } 
    }); 

    /////////////////////////////////////////////////////////////////////////////////////////// 
    /// CODIGO PARA QUITAR EL FOCO DEL PRIMER TEXTEDIT Y QUE NO SALGA EL TECLADO DE ANDROID /// 
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
    /////////////////////////////////////////////////////////////////////////////////////////// 

    //Thread thread = new Thread(this); 
    //thread.start(); 

    //mLooper = new MyLooper(); 
    //mLooper.start(); 

    ////////////////////////////////////////MANEJADOR SIMILAR A UN HILO, QUE ME PERMITE RECOGER MI POSICION ACTUAL Y ACTUALIZARLA EN EL MAPA UNA VEZ CADA 5 SEGUNDOS 
    mHandler = new Handler(); 
    queueRunnable(); 
    /////////////////////////////////////// FIN MANEJADOR 

} 
private void updateFriendPosition() 
{ 
    Bundle bundle = this.getIntent().getExtras();//get the intent & bundle passed by X 
    userText.setText(bundle.getString("user")); 
    permissionText.setText(bundle.getString("permission")); 
    lastUpdateText.setText(bundle.getString("lastupdate")); 
    String coordinates[] = {bundle.getString("lat"), bundle.getString("lon")}; 
    lat = Double.parseDouble(coordinates[0]); 
    lng = Double.parseDouble(coordinates[1]); 
    p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)); 
    OverlayItem overlayitem1 = new OverlayItem(p, bundle.getString("user"), "Hi Friend!"); 
    itemizedoverlay.addOverlay(overlayitem1); 
    mapOverlays.add(itemizedoverlay);// dibujo la estrella con la posicion actual del friend 

    mc.animateTo(p); ///nos centra el mapa en la posicion donde esta nuestro amigo 
    mc.setZoom(10); /// ajusta el zoom a 10   
} 
public void updateMyPosition() 
{ 
    Object LOCK = new Object(); 
    synchronized (LOCK){ 
    String coordinates[] = {settings.getString("mylatitude", null),settings.getString("mylongitude", null)};  
    lat = Double.parseDouble(coordinates[0]); 
    lng = Double.parseDouble(coordinates[1]); 
    p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)); 
    OverlayItem overlayitem1 = new OverlayItem(p, "Me", "My Position"); 
    itemizedoverlay2.addOverlay(overlayitem1); 
    mapOverlays.add(itemizedoverlay2);//dibujo mi icono para mostrarme a mi 
    } 
} 

回答

2

您正在修改的地圖的覆蓋列表。從文檔getOverlays()

如果您修改列表,你會 可能需要調用 View.postInvalidate(),使 變化將是可見的 用戶。

+0

我用mapView.invalidate()解決了它,它不重繪所有的地圖,只有項目! – NullPointerException 2010-11-22 10:22:56

相關問題