如何在谷歌地图android中创建多种颜色的多折线?

时间:2017-06-23 05:50:36

标签: android google-maps google-polyline

这可能会被问到很多次,但当我尝试使用不同的折线绘制多条路线时,没有一个解决方案对我有用我需要显示不同颜色的每条折线,但我得到的是黑色折线我没有&#39 ; t在任何地方使用让我发布我的地图屏幕截图

让我发布我的colde,使用google map api获取位置;

public class GetDistance extends AsyncTask<Double, Void, String> {
        private ProgressDialog pd;
        private static final int READ_TIMEOUT = 6000;
        private static final int CONNECTION_TIMEOUT = 6000;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd = new ProgressDialog(VisitTravel.this);
            pd.setMessage("Please wait");
            pd.show();
        }



        @Override
        protected String doInBackground(Double... strings) {
            URL url;
            try {
                url = new URL("http://maps.googleapis.com/maps/api/directions/json?origin=" + strings[0] + "," + strings[1] + "&destination=" + strings[2] + "," + strings[3] + "&sensor=false&units=metric&mode=driving&alternatives=true");

                HttpURLConnection conn;
                conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(READ_TIMEOUT);
                conn.setConnectTimeout(CONNECTION_TIMEOUT);


                conn.setRequestMethod("POST");

                InputStream in;

                in = new BufferedInputStream(conn.getInputStream());

                StringBuilder buffer = new StringBuilder();
                BufferedReader reader;
                reader = new BufferedReader(new InputStreamReader(in));
                String inputLine;
                while ((inputLine = reader.readLine()) != null)
                    buffer.append(inputLine).append("\n");
                if (buffer.length() == 0) {
                    // Stream was empty. No point in parsing.
                    Log.e("empty", "empty");
                }
                JsonResponse = buffer.toString();
                Log.d("response", JsonResponse);


            } catch (IOException e1) {
                e1.printStackTrace();
            }


            return JsonResponse;
        }
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            pd.dismiss();
            new ParserTask().execute(result);
        }

    } 

这是paresertask代码:

     private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {
            private ArrayList<LatLng> points;

            @Override
            protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {

                JSONObject jObject;
                List<List<HashMap<String, String>>> routes = null;

                try {
                    jObject = new JSONObject(jsonData[0]);
                    DirectionJSONParser parser = new DirectionJSONParser();

                    // Starts parsing data
                    routes = parser.parse(jObject);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return routes;
            }

            @Override
            protected void onPostExecute(List<List<HashMap<String, String>>> result) {
                PolylineOptions polylineOptionss=null;
                //      MarkerOptions markerOptions = new MarkerOptions();
                 // Traversing through all the routes
                for (int i = 0; i < result.size(); i++) {
                    points = new ArrayList<>();


                    // Fetching i-th route
                    List<HashMap<String, String>> path = result.get(i);

                    // Fetching all the points in i-th route
                    for (int j = 0; j < path.size(); j++) {
                        HashMap<String, String> point = path.get(j);

                        if (j == 0) {
                            duration = point.get("duration");
                            Log.d("duration", duration);
                            continue;
                        }
                        double lat = Double.parseDouble(point.get("lat"));
                        double lng = Double.parseDouble(point.get("lng"));
                        LatLng position = new LatLng(lat, lng);

                        points.add(position);

                    }
                     polylineOptionss=new PolylineOptions();
                    // Adding all the points in the route to LineOptions
                    polylineOptionss.addAll(points);
                  //  polylineOptions.width(7);
               //     Random rnd = new Random();
                 //   int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));

//Here only am creating polyline for different points but am getting black
                    if(i==0) {
                        PolylineOptions polylineOptions0=new PolylineOptions();
                        polylineOptions0.addAll(points);
                     //   mGoogleMap.setTrafficEnabled(true);
                        polylineOptions0.width(15);
                       polylineOptions0.color(Color.parseColor("#9c27b0"));
                        polylineOptions0.geodesic(true);
                        mGoogleMap.addPolyline(polylineOptions0);
                    }
                    else if(i==1){
                        PolylineOptions polylineOptions1=new PolylineOptions();
                        polylineOptions1.addAll(points);
                        polylineOptions1.geodesic(true);
                        polylineOptions1.width(15);
                     //   mGoogleMap.setTrafficEnabled(true);
                        mGoogleMap.addPolyline(polylineOptions1);
                        polylineOptions1.color(Color.parseColor("#ffffff"));
                    }
                    else if(i==2){
                        PolylineOptions polylineOptions2=new PolylineOptions();
                        polylineOptions2.addAll(points);
                        polylineOptions2.geodesic(true);
                        polylineOptions2.width(15);
                        mGoogleMap.addPolyline(polylineOptions2);
                   //     mGoogleMap.setTrafficEnabled(true);
                    polylineOptions2.color(Color.parseColor("#ffffff"));
                    }
                    else {
                        PolylineOptions polylineOptions3=new PolylineOptions();
                        polylineOptions3.addAll(points);
                  //      mGoogleMap.setTrafficEnabled(true);
                        polylineOptions3.width(15);
                        polylineOptions3.geodesic(true);
                        mGoogleMap.addPolyline(polylineOptions3);
                      polylineOptions3.color(Color.parseColor("#ffffff"));
                    }

                }
                CameraAnimation(polylineOptionss);
              //  mGoogleMap.addPolyline(polylineOptions);
                // Drawing polyline in the Google Map for the i-th route



            }

有人可以告诉我如何使用不同的颜色来区分折线。在此先感谢!!!

1 个答案:

答案 0 :(得分:1)

只需在polylineOptions.color();之前移动mGoogleMap.addPolyline()代码即可。  例如:

polylineOptions1.color(Color.parseColor("#ffffff"));
mGoogleMap.addPolyline(polylineOptions1);