Android – connect points on the map with lines

I have three GPS points in the android app. How do I set up a map to connect the first and second with the red and the second and third with the blue line? How to connect any two points on a map and draw a line between them?

resolvent:

This is a minimal implementation (only 2 points, no markers), which is extended with map overlay and 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;
   }
}

.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>