如何在Google Maps API v2上删除多条路线的直线

时间:2014-10-25 16:01:21

标签: java android google-maps

在我的应用程序中,我需要使用Google mapV2 API在源和目标之间显示地图和替代路线。 唯一的问题是,通过设置alternatives = true在地图上显示多条路线,一条直线与正确的路线一起出现。我想摆脱源和目的地之间的直线。

这是我的GMapV2GetRouteDirection代码,

public class GMapV2GetRouteDirection {
public final static String MODE_DRIVING = "driving";
public final static String MODE_WALKING = "walking";

public GMapV2GetRouteDirection() { }

public Document getDocument(LatLng start, LatLng end, String mode) {
    String url = "http://maps.googleapis.com/maps/api/directions/xml?"
            + "origin=" + start.latitude + "," + start.longitude 
            + "&destination=" + end.latitude + "," + end.longitude
            + "&sensor=false&units=metric&mode=driving&alternatives=true";

    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse response = httpClient.execute(httpPost, localContext);
        InputStream in = response.getEntity().getContent();
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(in);
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

这是我返回路线的方法,

private class GetRouteTask extends AsyncTask<String, Void, String> {

    private ProgressDialog Dialog;
    String response = "";

    @Override
    protected void onPreExecute() {
        Dialog = new ProgressDialog(MainActivity.this);
        Dialog.setMessage("Loading route...");
        Dialog.show();
    }

    @Override
    protected String doInBackground(String... urls) {
        // Get All Route values
        document = v2GetRouteDirection.getDocument(fromPosition,
                toPosition, GMapV2GetRouteDirection.MODE_DRIVING);
        response = "Success";
        return response;

    }

    @Override
    protected void onPostExecute(String result) {
        //mGoogleMap.clear();
        if (response.equalsIgnoreCase("Success")) {
            ArrayList<LatLng> directionPoint = v2GetRouteDirection
                    .getDirection(document);
            PolylineOptions rectLine = new PolylineOptions().width(8)
                    .color(Color.BLUE);


            /*for (int i = 0; i < directionPoint.size(); i++) {
                rectLine.addAll(directionPoint.get(i));
            }*/
            // Adding route on the map
            rectLine.addAll(directionPoint);
            mGoogleMap.addPolyline(rectLine);
            //markerOptions.position(toPosition);
            //markerOptions.draggable(true);
            //mGoogleMap.addMarker(markerOptions);

        }

1 个答案:

答案 0 :(得分:0)

wedak balagena inna moda prashna danne nethuwa