android地图无​​法看到两点

时间:2011-05-16 15:16:43

标签: java android google-maps

在我的应用中,我想在一张地图中显示两个纬度和长点。我得到一个值为当前位置lat和long以及其他值作为手动输入。我用不同的标记标记这两个点

但是我能够在地图中只看到一个标记,也就是当前位置。以下是我的代码,

MapView mapView;
    MapController mc,mc1;
    GeoPoint p,q;
    double latPoint,lngPoint;
    double latPoint1 = 9.909186;
    double lngPoint1 = 78.102936;

    class MapOverlay extends com.google.android.maps.Overlay
    {
        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;
            }

    } 

    class SitesOverlay extends com.google.android.maps.Overlay
    {
        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(q, screenPts);

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

    } 

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);

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

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

        mc = mapView.getController();
        mc1 = 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();

             }
             else
                    Log.e("GoogleMaps ","Error: Location  is null");
        }
        else
            Log.e("GoogleMaps ","Error: Location Manager is null");


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

        mc.animateTo(p);
        mc.setZoom(17);  
        mapView.invalidate();      


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

        q = new GeoPoint
        (
            (int) (latPoint1 * 1E6),
            (int) (lngPoint1 * 1E6)
         );

        mc1.animateTo(q);
        mc1.setZoom(17);  
        mapView.invalidate();    


        SitesOverlay sitesOverlay = new SitesOverlay();
        List<Overlay> listOfOverlays1 = mapView.getOverlays();
        listOfOverlays1.clear();
        listOfOverlays1.add(mapOverlay); 

        mapView.invalidate();

请帮帮我朋友......

1 个答案:

答案 0 :(得分:0)

以下是我用于在单个地图中获取两个位置的代码。一个是当前位置广告,另一个是我自己的

给出的值
public class ViewMap extends MapActivity
{
    MapView mapView;
    MapController mc,mc1;
    GeoPoint p,q;
    double latPoint,latPoint1,lngPoint,lngPoint1;
    private List<Overlay> mapOverlays;
    private Projection projection;
    int fromlatE6,fromlonE6,tolatE6,tolonE6;

class MapOverlay extends com.google.android.maps.Overlay
{
    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);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pin);
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);   

        mapView.getProjection().toPixels(q, screenPts);
        Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.marker);            
        canvas.drawBitmap(bmp1, screenPts.x, screenPts.y-50, null);

        return true;
    }  
} 

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mapview);

    latPoint1 = Double.valueOf(RestingSpotProfile.LAT);
    lngPoint1 = Double.valueOf(RestingSpotProfile.LON);

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

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

    mc = mapView.getController();
    mc1 = mapView.getController();

    LocationManager myManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    if(myManager != null)
    {
        String param = (String)myManager.getProviders(true).get(0);
        Location loc = myManager.getLastKnownLocation(param); 
        if(loc != null)
        {
            latPoint = loc.getLatitude();
            lngPoint = loc.getLongitude();

        }
        else
                Log.e("GoogleMaps ","Error: Location  is null");
    }
    else
        Log.e("GoogleMaps ","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);  

    q = new GeoPoint((int) ((latPoint1 + 2.0)* 1E6), (int) ((lngPoint1 + 2.0)* 1E6));

    tolonE6=(int)((latPoint + 2.0)*1e6);
    tolatE6=(int)((lngPoint + 2.0)*1e6);

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

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

    mapView.invalidate();      

    mc1.animateTo(q);
    mc1.setZoom(17);  
    mapView.invalidate();    

    mapView.invalidate();
}

@Override
protected boolean isRouteDisplayed()
{
    return false;
}

public class MyOverlay extends Overlay
{
    private GeoPoint gp1;
    private GeoPoint gp2;

    public MyOverlay(int fromlatE6,int fromlonE6,int tolatE6,int tologE6) 
    {               
        int flat=0,flog=0,tlat=0,tlog=0;
        flat=fromlatE6;
        flog=fromlonE6;
        tlat=tolatE6;
        tlog=tologE6;

        gp1 = new GeoPoint(flat,flog);
        gp2 = new GeoPoint(tlat,tlog);
    }
}

}