Как создать многополилинию с несколькими цветами в карте Google для Android?

Это может быть задано много раз, но ни одно из решений не сработало для меня, когда я пытаюсь построить несколько маршрутов с разными полилиниями, мне нужно показать каждую полилинию разным цветом, но я получаю полилинию черного цвета, которую я нигде не использовал пусть я выкладываю свой скрин карты

Позвольте мне опубликовать мой холод, я использую 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);
        }

    } 

Это код парсертаска:

     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



            }

Может кто-нибудь, пожалуйста, скажите мне, как различать полилинию с разными цветами, я борюсь с этим. Заранее спасибо!!!


person M.Yogeshwaran    schedule 23.06.2017    source источник


Ответы (1)


Просто переместите свой код polylineOptions.color(); перед mGoogleMap.addPolyline(). Например:

polylineOptions1.color(Color.parseColor("#ffffff"));
mGoogleMap.addPolyline(polylineOptions1);
person antonio    schedule 23.06.2017