0

我一直在尝试这个,所以我在这里寻求一些认真的帮助!

在我的应用程序中,我想在单击按钮时绘制超过 2 条替代路线,例如在我的应用程序中,我有 3 个按钮,例如 r1、r2 和 r3。当我在应用程序各自的编辑文本中输入源位置和目标位置并单击 Enter 时然后我将在默认情况下绘制 r1 所以现在当我点击 r2 按钮时,之前绘制在地图上的路径/路线应该被删除/删除,并且应该在地图上绘制新的替代路径/路线,但不是那样发生而不是删除以前的路径/路线它正在地图上绘制两条路线:(有人可以帮我吗?

这是我的代码。

List<RoutesDictionary> routeDicList = new ArrayList<RoutesDictionary>();

routeTwo.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {



        map.clear();
        routeDicList.clear();
        routeType = 2;
        rout = "walking";
        new TrafficData().execute();

        Log.d("TH routeTwo", "In routeTwo After" +
        `enter code here`routeDicList.size());

    }
});


public class TrafficData extends AsyncTask<Context, String, String> {

    routeTwo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            map.clear();
            routeDicList.clear();
            routeType = 2;
            rout = "walking";
            new TrafficData().execute();

        }
    });

    @Override
    protected String doInBackground(Context... params) {

        try {

            if (!via_route.equals("")) {
                url = "http://maps.googleapis.com/maps/api/directions/json?origin="
                        + sourceAdd
                        + "&destination="
                        + destinationAdd
                        + "&waypoints=via:"
                        + via_route
                        + "&sensor=false&mode=" + rout;
            } else {
                url = "http://maps.googleapis.com/maps/api/directions/json?origin="
                        + sourceAdd
                        + "&destination="
                        + destinationAdd
                        + "&sensor=false&mode=" + rout;

            }

            // Making HTTP request
            try {
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet httpPost = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpPost);

                result = EntityUtils.toString(httpResponse.getEntity());
                Log.v("Traffic App : ", "The Traffic App result is : "
                        + result);

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;

    }

    @Override
    protected void onPostExecute(String result) {

        super.onPostExecute(result);

        Log.v("Traffic App : ", "The Traffic App resultTraffic  is : "
                + resultTraffic);
        try {

            JSONObject routesObj = new JSONObject(result);
            String status = routesObj.getString("status");
            if (status.equals("OK")) {
                routesDictionary = new RoutesDictionary();
                allRouteDic.addSrcAddress(sourceAdd);
                allRouteDic.addDesAddress(destinationAdd);

                AllRoutes.allRoutes.add(allRouteDic);

                Log.v("***********************", "THE ***** Array list : ");

                JSONArray routesArr = routesObj.getJSONArray("routes");
                JSONObject mainObj = routesArr.getJSONObject(0);

                JSONArray legsArr = mainObj.getJSONArray("legs");
                int legsSize = legsArr.length();

                for (int i = 0; i < legsSize; i++) {

                    routesDictionary.addlegsDistance(legsArr.getJSONObject(
                            i).getJSONObject("distance"));
                    routesDictionary.addlegsDuration(legsArr.getJSONObject(
                            i).getJSONObject("duration"));
                    routesDictionary.addlegsEndaddress(legsArr
                            .getJSONObject(i).getString("end_address"));
                    routesDictionary
                            .addlegsEndlocation(legsArr.getJSONObject(i)
                                    .getJSONObject("end_location"));
                    routesDictionary.addlegsStartaddress(legsArr
                            .getJSONObject(i).getString("start_address"));
                    routesDictionary.addlegsStartlocation(legsArr
                            .getJSONObject(i).getJSONObject(
                                    "start_location"));

                    routeDicList.add(routesDictionary);

                    JSONArray stepsArr = legsArr.getJSONObject(i)
                            .getJSONArray("steps");

                    int stepsLen = stepsArr.length();

                    for (int j = 0; j < stepsLen; j++) {
                        routesDictionary
                                .addlegs_StepsDistance(stepsArr
                                        .getJSONObject(j).getJSONObject(
                                                "distance"));
                        routesDictionary
                                .addlegs_StepsDuration(stepsArr
                                        .getJSONObject(j).getJSONObject(
                                                "duration"));
                        routesDictionary.addlegs_StepsEndlocation(stepsArr
                                .getJSONObject(j).getJSONObject(
                                        "end_location"));
                        routesDictionary.addlegs_StepsHtmlInstruct(stepsArr
                                .getJSONObject(j).getString(
                                        "html_instructions"));
                        routesDictionary
                                .addlegs_StepsPloyline(stepsArr
                                        .getJSONObject(j).getJSONObject(
                                                "polyline"));
                        routesDictionary
                                .addlegs_StepsStartlocation(stepsArr
                                        .getJSONObject(j).getJSONObject(
                                                "start_location"));
                        routesDictionary.addlegs_StepsTravelmode(stepsArr
                                .getJSONObject(j).getString("travel_mode"));

                        routeDicList.add(routesDictionary);
                    }

                }

                int strLoc = routeDicList.get(0)
                        .getlegs_StepsStartlocation().size();
                for (int k = 0; k < strLoc; k++) {
                    routesDictionary.addStrLatit(routeDicList.get(0)
                            .getlegs_StepsStartlocation().get(k)
                            .getString("lat"));
                    routesDictionary.addStrlongi(routeDicList.get(0)
                            .getlegs_StepsStartlocation().get(k)
                            .getString("lng"));

                    routeDicList.add(routesDictionary);

                }

                int endLoc = routeDicList.get(0).getlegs_StepsEndlocation()
                        .size();
                for (int l = 0; l < endLoc; l++) {
                    routesDictionary.addEndLatit(routeDicList.get(0)
                            .getlegs_StepsEndlocation().get(l)
                            .getString("lat"));
                    routesDictionary.addEndLongi(routeDicList.get(0)
                            .getlegs_StepsEndlocation().get(l)
                            .getString("lng"));

                    routeDicList.add(routesDictionary);
                }

                int strL = routeDicList.get(0).getStrLatit().size();
                for (int m = 0; m < strL; m++) {
                    Log.v("Traffic : ", "***  Latitude and Longi *** : "
                            + routeDicList.get(0).getStrLatit().get(m)
                            + "       "
                            + routeDicList.get(0).getStrLong().get(m));
                }

                int endL = routeDicList.get(0).getEndLatit().size();
                for (int m = 0; m < endL; m++) {
                    Log.v("Traffic : ", "###  Latitude and Longi ### : "
                            + routeDicList.get(0).getEndLatit().get(m)
                            + "       "
                            + routeDicList.get(0).getEndLong().get(m));
                }


                rectOptions = new PolylineOptions();

                Log.v("*****************", "The RECTPOINTS SIZE : "
                        + rectOptions.getPoints().size());

                rectOptions.getPoints().clear();

                switch (routeType) {
                case 1:
                    rectOptions.color(Color.RED);
                    break;

                case 2:
                    rectOptions.color(Color.BLUE);
                    break;

                case 3:
                    rectOptions.color(Color.GREEN);
                    break;

                default:
                    break;
                }


                rectOptions.width(4);

                for (int i = 1; i < strL; i++) {
                    rectOptions.add(new LatLng(Double.valueOf(routeDicList
                            .get(0).getStrLatit().get(i)), Double
                            .valueOf(routeDicList.get(0).getStrLong()
                                    .get(i))));

                }

                // Get back the mutable Polygon
                map.clear();
                polyline = map.addPolyline(rectOptions.geodesic(false));
                polyline.remove();
                polyline = map.addPolyline(rectOptions.geodesic(true));

                double lng = Double.valueOf(routeDicList.get(0)
                        .getlegs_StepsStartlocation().get(0)
                        .getString("lng"));
                double lat = Double.valueOf(routeDicList.get(0)
                        .getlegs_StepsStartlocation().get(0)
                        .getString("lat"));

                map.animateCamera(CameraUpdateFactory.newLatLngZoom(
                        new LatLng(lat, lng), 14.0f));

                // start longitude and latitude
                double startlng = Double
                        .valueOf(routeDicList.get(0)
                                .getlegs_StepsEndlocation().get(0)
                                .getString("lng"));
                double startlat = Double
                        .valueOf(routeDicList.get(0)
                                .getlegs_StepsEndlocation().get(0)
                                .getString("lat"));

                // start location marker
                addMarker(startlat, startlng);

                // end longitude and latitude
                double endlng = Double.valueOf(routeDicList.get(0)
                        .getlegs_StepsStartlocation().get(endLoc - 1)
                        .getString("lng"));
                double endlat = Double.valueOf(routeDicList.get(0)
                        .getlegs_StepsStartlocation().get(endLoc - 1)
                        .getString("lat"));

                // end location marker
                addMarker(endlat, endlng);

            }

        } catch (JSONException e) {

            e.printStackTrace();
        }

    }
4

1 回答 1

0

1-您将OnClickListenerfor routeTwo按钮设置了两次,一个在主类中,一个在AsyncTask中,应该删除AsyncTask中的一个。2-快速按下按钮两次会产生问题,因为您将启动两个可分开的AsyncTask,每个将绘制一条路线,因此两条路线都会显示,我更喜欢只使用一个AsyncTask作为全局变量,然后在运行新的之前检查是否有是一个正在运行的AsyncTask,这可以通过

if(trafficData != null && trafficData.getStatus() != AsyncTask.Status.FINISHED){
trafficData .cancel(true);
}
于 2013-09-20T20:49:17.827 回答