2012-04-26 118 views
3

我想在我的地圖項目上繪製一條線,但無法繪製線條。我在哪裏以及如何聲明覆蓋圖?我嘗試了各種方法,但無法讓它工作。即代碼只是在Eclipse中顯示錯誤。我不想做的是畫一條從A到B的路線,而是在我移動時繪製路線。如何在谷歌地圖上繪製線條疊加Android

// Creating a MapView 
public class Gpstrack extends MapActivity { 
    private MapView map; 
    private MapController controller; 
    private Projection projection; 
    ArrayList<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>(); 

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

     setContentView(R.layout.main); 

     initMapView(); 
     initMyLocation(); 


     TabHost.TabSpec spec; 

     TabHost th = (TabHost)findViewById(R.id.tabhost); 
     th.setup(); 

     spec = th.newTabSpec("tag1"); 
     spec.setContent(R.id.map_Tab); 
     spec.setIndicator("Map"); 
     th.addTab(spec); 

     spec = th.newTabSpec("tag2"); 
     spec.setContent(R.id.log_Tab); 
     spec.setIndicator("Log"); 
     th.addTab(spec); 

     spec = th.newTabSpec("tag3"); 
     spec.setContent(R.id.details_Tab); 
     spec.setIndicator("Details"); 
     th.addTab(spec); 

     spec = th.newTabSpec("tag4"); 
     spec.setContent(R.id.student_Tab); 
     spec.setIndicator("Student Info"); 
     th.addTab(spec); 

    } 

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

    //Map and Controls 
    private void initMapView() { 
     map = (MapView) findViewById(R.id.mvMain); 
     controller = map.getController(); 
     map.setSatellite(true); 
     //map.setStreetView(true); 
     map.setBuiltInZoomControls(true); 
    } 


    //Creates an Overlay that marks current position 
    private void initMyLocation() { 
     final MyLocationOverlay overlay = new MyLocationOverlay(this, map); 

     overlay.enableMyLocation(); 
     overlay.enableCompass(); 
     overlay.runOnFirstFix(new Runnable() { 
      public void run() { 
       controller.setZoom(17); 
       controller.animateTo(overlay.getMyLocation()); 
       map.getOverlays().add(overlay); 



      } 

     }); 

    } 


    //Experiment 
    class MyOverlay extends Overlay { 
     public void draw(Canvas canvas, MapView mapv, boolean shadow) { 
      super.draw(canvas, mapv, shadow); 
      Projection projection = mapv.getProjection(); 
      Path p = new Path(); 



      for (int i = 0; i < geoPointsArray.size(); i++) { 
       if (i == geoPointsArray.size() -1) { 
        break; 
       } 
       Point from = new Point(); 
       Point to = new Point(); 
       projection.toPixels(geoPointsArray.get(i), from); 
       projection.toPixels(geoPointsArray.get(i + 1), to); 
       p.moveTo(from.x, from.y); 
       p.lineTo(to.x, to.y); 
       } 

      Paint mPaint = new Paint(); 
      mPaint.setStyle(Style.STROKE); 
      mPaint.setColor(Color.GREEN); 
      mPaint.setAntiAlias(true); 
      mPaint.setStrokeWidth(5); 
      canvas.drawPath(p, mPaint); 
      mapv.invalidate(); 
      super.draw(canvas, mapv, shadow); 


     } 
    }  
} 
+1

什麼是錯誤告訴我們你的日誌貓。 – Nitin 2012-04-26 06:47:22

+0

這不是一個輸出錯誤,它是說「方法添加(疊加)類型列表不適用於論據(boolen) – sKwok12 2012-04-26 16:23:13

+0

我認爲你應該實現位置更改偵聽器 – Nitin 2012-04-27 06:26:09

回答

1
+0

你能指出在哪裏我需要聲明覆蓋圖的代碼? – sKwok12 2012-04-26 04:33:34

+0

在一個單獨的類文件中定義覆蓋圖,然後在MapActivity中的OnCreate中聲明它的實例,將它添加到地圖覆蓋圖 – Habib 2012-04-26 04:45:32

+0

我將無法在同一個類文件中執行此操作嗎? – sKwok12 2012-04-26 17:55:01

1

你有沒有這個演示嘗試實現Google Map Overlay

希望你有了這個演示的想法。

享受。 :)

0

在你覆蓋類,你可以畫線這樣

public class MapOverlay extends com.google.android.maps.Overlay 

    { 
     Canvas canvas; 

     @Override 
     public boolean draw(Canvas canvas, MapView mapView, boolean shadow, 
       long when) 
     { 
      // TODO Auto-generated method stub 
      super.draw(canvas, mapView, shadow); 
      this.canvas=canvas; 
      Point screenpoint = new Point(); 

      mapView.getProjection().toPixels(p, screenpoint); 
      Bitmap bmp = BitmapFactory.decodeResource(
         getResources(), R.drawable.pushpin);    
        canvas.drawBitmap(bmp, canvas.getWidth()/4, 
          canvas.getHeight()/4, null); 

        Paint paint = new Paint(); 
        paint.setColor(Color.BLACK); 
        canvas.drawLine(canvas.getWidth()/4, canvas.getHeight()/4, 
          canvas.getWidth()/2, canvas.getHeight()/2, paint); 
        return true; 

     } 


     return true; 
    }  



    } 

拉線功能就像是

public void drawLine (float startX, float startY, 
float stopX, float stopY, Paint paint) 
    Since: API Level 1 Draw a line segment with the specified start and stop x,y coordinates, using the specified paint. 

    Parameters 
    startX The x-coordinate of the start point of the line 
    startY The y-coordinate of the start point of the line 
    paint The paint used to draw the line 

,如果你想畫的路徑,你可以reffer這個quetion

J2ME/Android/BlackBerry - driving directions, route between two locations

相關問題