2012-03-12 60 views
0

我想把標記放在地圖上,我已經做到了,但問題在於它顯示了所有標記。以前的&最近也是。我不想顯示以前的標記,而是我只是要顯示在其上點擊recently.All其它標記不應該被看作谷歌地圖標記錯誤

我寫的代碼與follwing教程

http://mobiforge.com/developing/story/using-google-maps-android

的幫助,我的代碼是

的地方標記
public class SelectLocation extends MapActivity { 
MapView mapView; 
MapController mc; 
GeoPoint p; 
String coordinates[]; 


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

    private GeoPoint p; 
    public MapOverlay(GeoPoint p){ 
     this.p = p; 
    } 

    @Override 
    public boolean draw(Canvas canvas, MapView mapView, 
      boolean shadow, long when) 
    { 
     super.draw(canvas, mapView, shadow);     

     //---translate the GeoPoint to screen pixels--- 
     Point screenPts = new Point(); 
     mapView.getProjection().toPixels(p, screenPts); 

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

    public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    { 
     //---when user lifts his finger--- 
     if (event.getAction() == 1) {     
      GeoPoint p = mapView.getProjection().fromPixels(
        (int) event.getX(), 
        (int) event.getY()); 

      Toast.makeText(getBaseContext(), 
        p.getLatitudeE6()/1E6 + "," + 
          p.getLongitudeE6() /1E6 , 
          Toast.LENGTH_SHORT).show(); 
      mapView.getOverlays().add(new MapOverlay(p)); 
      Intent i=new Intent(SelectLocation.this,SetLocat.class); 
      startActivity(i); 

     }        
     return false; 
    }   

} 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.selectlocation); 

    mapView = (MapView) findViewById(R.id.mapView); 

    mapView.setBuiltInZoomControls(true); 

    mc = mapView.getController(); 
    String coordinates[] = {"1.352566007", "103.78921587"}; 
    double lat = Double.parseDouble(coordinates[0]); 
    double lng = Double.parseDouble(coordinates[1]); 

    p = new GeoPoint(
      (int) (lat * 1E6), 
      (int) (lng * 1E6)); 

    mc.animateTo(p); 
    mc.setZoom(17); 
    MapOverlay mapOverlay = new MapOverlay(p); 


    List<Overlay> listOfOverlays = mapView.getOverlays(); 
    listOfOverlays.clear(); 
    listOfOverlays.add(mapOverlay);   




} 
/*protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
}*/ 
@Override 
protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
} 

回答

0

這個代碼

mapView.getOverlays().add(new MapOverlay(p)); 

嘗試之前,

mapView.getOverlays().clear(); 
mapView.invalidate(); 
mapView.getOverlays().add(new MapOverlay(p)); 
+0

由於其工作正常 – user1196969 2012-03-12 07:10:56

+0

好了:)如果它解決了你的問題,你可以隨時通過點擊左邊的刻度線接受答案回答。 – MKJParekh 2012-03-12 07:13:06