2011-05-18 45 views
0

我需要在地圖上標記兩個點,一個是當前位置,第二個是我給的。在我的代碼中僅顯示第二點。第一點不顯示。請幫幫我。 我的代碼:在Android的地圖上的兩個標記問題

public class MapsActivity extends MapActivity 
{  
MapView mapView; 
MapController mc; 
GeoPoint p; 
double latPoint, lngPoint; 
class MapOverlay extends com.google.android.maps.Overlay 
{ 
    @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.pin);    
     canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);   
     return true; 
    } 
} 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    mapView = (MapView) findViewById(R.id.mapView); 
    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom); 
    View zoomView = mapView.getZoomControls(); 

    zoomLayout.addView(zoomView, 
     new LinearLayout.LayoutParams(
      LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT)); 
    mapView.displayZoomControls(true); 

    mc = mapView.getController(); 
    LocationManager myManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     if(myManager != null){ 
      //List list = myManager.getAllProviders(); 
      String param = (String)myManager.getProviders(true).get(0); 
      Location loc = myManager.getLastKnownLocation(param); 
      if(loc != null){ 
       latPoint = loc.getLatitude(); 
       lngPoint = loc.getLongitude(); 
       Log.e("map",latPoint+" "+lngPoint); 
      } 
      else 
        Log.e("RootDraw ","Error: Location is null"); 
     } 
     else 
      Log.e("RootDraw ","Error: Location Manager is null"); 

    p = new GeoPoint((int) (latPoint * 1E6), (int) (lngPoint * 1E6)); 


    //---Add a location marker--- 
    MapOverlay mapOverlay = new MapOverlay(); 
    List<Overlay> listOfOverlays = mapView.getOverlays(); 
    listOfOverlays.clear(); 
    listOfOverlays.add(mapOverlay);  

    // second point: 
     latPoint=x.xxxxxxxxxxxx; // nearest to current locattion 
     lngPoint=x.xxxxxxxxxxxx; // nearest to current locattion 
    p = new GeoPoint((int) (latPoint * 1E6), (int) (lngPoint * 1E6)); 

    mc.animateTo(p); 
    mc.setZoom(9); 

    //---Add a location marker--- 
    MapOverlay mapOverlay = new MapOverlay(); 
    List<Overlay> listOfOverlays = mapView.getOverlays(); 
    listOfOverlays.clear(); 
    listOfOverlays.add(mapOverlay); 

    mapView.invalidate(); 
} 

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

回答

0

嘗試創建2個GeoPoints對象,然後在draw功能,使用下面的代碼:

@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.pin);    
     canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);  

     mapView.getProjection().toPixels(p1, screenPts); 

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



     return true; 
    } 
} 
+0

感謝它的工作原理 – 2011-05-18 12:10:22