2012-04-26 79 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

相关问题