проблема в два маркера на карте в Android - PullRequest
0 голосов
/ 18 мая 2011

Мне нужно отметить две точки на карте, одна - текущее местоположение, а вторая - то, что я даю. в моем коде только второй пункт показан. Первая точка не показывает. Помогите мне, пожалуйста. мой код:

 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;
}
 }

1 Ответ

0 голосов
/ 18 мая 2011

Попробуйте создать 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;
    }
} 
...