2011-05-16 67 views
0

在我的應用程序中,我想在單個地圖中顯示兩個經緯度。我得到一個值作爲當前經緯度,其他值作爲手動輸入。我用不同的標記標記這兩個點安卓地圖無法看到兩個點

但是我能夠在我的地圖中看到只有一個標記,也就是當前的位置。以下是我的代碼,

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(); 

請幫我的朋友......

回答

0

下面是我用在一個單一的地圖得到兩個位置的代碼。一是當前位置的廣告,另一個值給出b我自己

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

}