Connecting points on a map with lines

I have three gps points in an android app. How to install on the card to connect the first and second with red, the second and third with a blue line? How to connect any two points on the map, draw a line between them?

+3
source share
1 answer

Here's the minimal implementation (only 2 points, no labels) using map overlay and mapView.getProjection () for extension:

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

.

+13
source

Source: https://habr.com/ru/post/1790083/


All Articles