2011-05-18 47 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