用线条连接地图上的点

时间:2011-02-04 21:13:10

标签: android

我在Android应用程序中有三个GPS点数。如何设置地图连接第一和第二与红色和第二和第三与蓝线?如何在地图上连接任意两点,在它们之间画线?

1 个答案:

答案 0 :(得分:13)

这是一个最小的实现(仅限2个点,没有标记),使用地图叠加层和mapView.getProjection()来扩展:

    public class HelloGoogleMaps extends  MapActivity  {
    /** Called when the activity is first created. */

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MapController mMapController;
        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mMapController = mapView.getController();
        mMapController.setZoom(18);
      // Two points in Mexico about 1km apart
        GeoPoint point1 = new GeoPoint(19240000,-99120000);
        GeoPoint point2 = new GeoPoint(19241000,-99121000);
        mMapController.setCenter(point2);
        // Pass the geopoints to the overlay class
        MapOverlay mapOvlay = new MapOverlay(point1, point2);
        mapView.getOverlays().add(mapOvlay);
    }

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

      private GeoPoint mGpt1;
      private GeoPoint mGpt2;

      protected MapOverlay(GeoPoint gp1, GeoPoint gp2 ) {
         mGpt1 = gp1;
         mGpt2 = gp2;
      }
      @Override
      public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {
         super.draw(canvas, mapView, shadow);
         Paint paint;
         paint = new Paint();
         paint.setColor(Color.RED);
         paint.setAntiAlias(true);
         paint.setStyle(Style.STROKE);
         paint.setStrokeWidth(2);
         Point pt1 = new Point();
         Point pt2 = new Point();
         Projection projection = mapView.getProjection();
         projection.toPixels(mGpt1, pt1);
         projection.toPixels(mGpt2, pt2);
         canvas.drawLine(pt1.x, pt1.y, pt2.x, pt2.y, paint);
         return true;
      }
   }
   @Override
   protected boolean isRouteDisplayed() {
      // TODO Auto-generated method stub
      return false;
   }
}