2017-06-23 99 views
0

這可能會問很多時間,但沒有解決方案爲我工作,當我嘗試繪製不同折線的多條路線,我需要顯示不同顏色的每條折線,但得到的是我沒有使用的黑色折線任何地方讓我發佈我的地圖的屏幕截圖如何在Google地圖android中使用多種顏色創建多重摺線?

讓我發佈我的colde,我使用谷歌地圖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

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

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

多麼愚蠢我是你,是偉大的antonio非常感謝! –

相關問題