删除或删除google map api v2中的折线

时间:2013-09-20 06:34:42

标签: android google-maps google-maps-android-api-2

我很长时间都在尝试这个,所以我在这里寻求一些认真的帮助!!

在我的应用程序中,我想在点击一个按钮时绘制两个以上的替代路线,比如在我的应用程序中我有3个按钮,例如r1,r2和r3。当我在我的应用程序各自的edittexts中输入源位置和目标位置时然后单击输入然后我将默认情况下绘制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();
        }

    }

1 个答案:

答案 0 :(得分:0)

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

完成
if(trafficData != null && trafficData.getStatus() != AsyncTask.Status.FINISHED){
trafficData .cancel(true);
}