如何使用复选框在地图上显示折线?

时间:2018-04-14 17:42:47

标签: android google-maps polyline

我想知道当我点击复选框时如何在地图上显示折线?

1 个答案:

答案 0 :(得分:0)

定义折线对象

Polyline polyLine;

使用下面给出的这个类来调整直线,只需将FROM传递给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
        }
    }
}

`