2011-11-01 39 views
0

在我的應用程序中,我正在顯示世界地圖。
當用戶點擊一個特定的位置時,我附加了一個標記。(Android谷歌地圖)刪除舊標記,並在用戶使用地圖疊加單擊新位置時放置新標記

但是,當用戶點擊不同的位置,我希望刪除以前的標記並將其附加到新的位置。

我檢查了幾個帖子,但不能拿出任何解決方案。

下面是代碼:

class MapOverlay extends com.google.android.maps.Overlay 
{ 
    @Override 
    public boolean draw(Canvas canvas, MapView mapView, 
    boolean shadow, long when) 
    { 

     return false; 

    } 
@Override 
    public boolean onTap(GeoPoint p, MapView mapView) { 
// TODO Auto-generated method stub 
List<Overlay> mapOverlays = mapView.getOverlays(); 
Drawable drawable = TpActivity.this.getResources().getDrawable(R.drawable.marker); 
    HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,TpActivity.this); 
OverlayItem overlayitem = new OverlayItem(p, "", ""); 
itemizedoverlay.addOverlay(overlayitem); 
mapOverlays.add(itemizedoverlay); 
mapView.invalidate(); 

latitude=p.getLatitudeE6()/1E6; 
longitude=p.getLongitudeE6() /1E6 ; 
     Toast.makeText(TpActivity.this, 
      p.getLatitudeE6()/1E6 + "," + 
      p.getLongitudeE6() /1E6 , 
      Toast.LENGTH_SHORT).show(); 

     new AlertDialog.Builder(TpActivity.this) 
     .setTitle("Change city") 
     .setMessage("go to the new location?") 
     .setNegativeButton("NO", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO Auto-generated method stub 
       dialog.dismiss(); 
      } 
     }) 
     .setPositiveButton("YES", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO Auto-generated method stub 
      new newasync().execute(); 
      } 
     }).show(); 
return true; 
} 
+0

我的基本目標是允許用戶選擇地圖上的任何位置,並將標記附加到用戶觸摸的最新位置......以使他能夠視覺確認他觸摸了哪個位置 –

回答

1

我用覆蓋類,而不是itemizedoverlay 這裏的繪製函數的代碼是:

Geopoint k; 

的OnCreate代碼在這裏:

mapview = (MapView) findViewById(R.id.mapView); 
     mapview.setBuiltInZoomControls(true); 
     List<Overlay> mapOverlays = mapview.getOverlays(); 
     MapOverlay mapOverlay = new MapOverlay(); 

     mapOverlays.add(mapOverlay); 

覆蓋圖類別:

class MapOverlay extends com.google.android.maps.Overlay 
{  


    @Override 
    public boolean onTap(GeoPoint p, MapView mapView) { 
     // TODO Auto-generated method stub 
     Drawable drawable = TpActivity.this.getResources().getDrawable(R.drawable.marker); 
     /* List<Overlay> mapOverlays = mapView.getOverlays(); 

      HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,TpActivity.this); 


     OverlayItem overlayitem = new OverlayItem(p, "", ""); 

     itemizedoverlay.addOverlay(overlayitem); 

     mapOverlays.add(itemizedoverlay);*/ 
     k = p; 
     mc= mapView.getController(); 
     mc.animateTo(p); 
     // Point pt=mapView.getProjection().toPixels(p, null); 
     mapView.invalidate(); 
     latitude=p.getLatitudeE6()/1E6; 
     longitude=p.getLongitudeE6() /1E6 ; 
       Toast.makeText(TpActivity.this, 
        p.getLatitudeE6()/1E6 + "," + 
        p.getLongitudeE6() /1E6 , 
        Toast.LENGTH_SHORT).show(); 

       new AlertDialog.Builder(TpActivity.this) 
       .setTitle("Change city") 
       .setMessage("go to the new location?") 
       .setNegativeButton("NO", new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // TODO Auto-generated method stub 
         dialog.dismiss(); 
        } 
       }) 
       .setPositiveButton("YES", new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // TODO Auto-generated method stub 
        new newasync().execute(); 
        } 
       }).show(); 
     return true; 
    } 




    @Override 
    public boolean draw(Canvas canvas, MapView mapView, 
    boolean shadow, long when) 
    { 
     super.draw(canvas, mapView, shadow);     
     if(k!=null) 
     { 
     //---translate the GeoPoint to screen pixels--- 
     Point screenPts = new Point(); 
     mapView.getProjection().toPixels(k, screenPts); 

     //---add the marker--- 
     Bitmap bmp = BitmapFactory.decodeResource(
      getResources(), R.drawable.marker);    
     canvas.drawBitmap(bmp, screenPts.x-10, screenPts.y-34, null); 
     } 
     return true; 


    }