-1

I was wondering how can I make polylines to show on my map when I click on checkbox?

1 Answers1

0

Define object of polyline

Polyline polyLine;

Use this class given below to drab the lines just pass your FROM to TO lat,lng

private class MapDirectionAsync extends AsyncTask<Void, Void, PolylineOptions> {
    private LatLng
            sourceLatLng,
            destinationLatLng;

    private MapDirectionAsync(LatLng sourceLatLng, LatLng destinationLatLng) {
        this.sourceLatLng = sourceLatLng;
        this.destinationLatLng = destinationLatLng;
    }

    @Override
    protected PolylineOptions doInBackground(Void... params) {
        GMapV2Direction md = new GMapV2Direction();
        Document doc = md.getDocument(sourceLatLng, destinationLatLng, GMapV2Direction.MODE_DRIVING);
        if (doc != null) {
            ArrayList<LatLng> directionPoint = md.getDirection(doc);
            PolylineOptions rectLine = new PolylineOptions().width(16).color(ContextCompat.getColor(TripsActivity.this, R.color.colorPrimary));

            float km = Float.parseFloat(md.getDistanceText(doc)) / 1000;
            String km1 = String.format(Locale.US, "%.1f", km);
            Log.e("GoogleMapRoute", "Total KM : " + km1);

            int duration = md.getDurationValue(doc);
            Log.e("GoogleMapRoute", "===============Total Duration : " + duration / 60);

            for (int i = 0; i < directionPoint.size(); i++) {
                rectLine.add(directionPoint.get(i));
            }
            return rectLine;
        }

        return null;
    }

    @Override
    protected void onPostExecute(PolylineOptions rectLine) {
        super.onPostExecute(rectLine);

        if (rectLine != null) {
            polyLine = google_Map.addPolyline(rectLine);
        } else {
            // No polyLine available
        }
    }
}

`

Jay Thummar
  • 2,281
  • 1
  • 14
  • 22