title: Polylines are not showing between 2 Locations but Time and Distance is showing
body: <p><strong>Note: Sorry for my bad English because English is not my native language I have used Google Translate for this.</strong></p>
  
  <p>I am working on an Android map app in which a user can find their friends' current location. For storing the current location I am using Firebase real time database. With previous help of Lalit Singh it is showing time and distance now but  still not updating the Polylines between 2 different places. My updates code is below.</p>
  
  <p>mapsActivity.java (where i am getting friend location from fire base and updated time, distance and polylines method called)</p>
  
  <blockquote>
  <pre><code>  public void proceed(final View view) {
  
      DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Email");
  
      ref.addValueEventListener(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                  UserInformation details = dataSnapshot1.getValue(UserInformation.class);
  
                  if (mMap != null) {
                      mMap.clear();
                  }
  
                  MarkerOptions markerOptions = new MarkerOptions();
                  LatLng latLng1 = new LatLng(details.getLatitude(), details.getLongitude());
                  markerOptions.position(latLng1);
                  markerOptions.title(String.valueOf(latLng1));
                  mMap.addMarker(markerOptions).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
                  mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                  mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng1));
                  mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
  
                  distance_task.getDirectionsUrl(latLng1, markerOptions.getPosition());
                  distance_task.setLoadListener(new CalculateDistanceTime.taskCompleteListener() {
                      @Override
                      public void taskCompleted(String[] time_distance) {
                          text1.setText(time_distance[0]); //Distance
                          text2.setText(time_distance[1]); //Time
                      }
                  });
  
                  String url = getDirectionsUrl(latLng1, markerOptions.getPosition());
                  DownloadTask downloadTask = new DownloadTask();
                  downloadTask.execute(url);
              }
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
          }
      });
  }
  </code></pre>
  </blockquote>
  
  <p>CalculateDistanceTime.java (added new Class)</p>
  
  <blockquote>
  <pre><code>class CalculateDistanceTime {     private taskCompleteListener
  mTaskListener; private Context mContext;      
  CalculateDistanceTime(Context context) {
  mContext = context; }     void setLoadListener(taskCompleteListener taskListener) {
  mTaskListener = taskListener; }        void getDirectionsUrl(LatLng origin, LatLng dest) {
  
  // Origin of route
  String str_origin = "origin=" + origin.latitude + "," + origin.longitude;
  
  // Destination of route
  String str_dest = "destination=" + dest.latitude + "," + dest.longitude;
  
  
  // Sensor enabled
  String sensor = "sensor=false";
  
  // Building the parameters to the web service
  String parameters = str_origin + "&amp;" + str_dest + "&amp;" + sensor;
  
  // Output format
  String output = "json";
  
  // Building the url to the web service
  String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;
  
  
  DownloadTask downloadTask = new DownloadTask();
  
  // Start downloading json data from Google Directions API
  
  downloadTask.execute(url); }
  
  private String downloadUrl(String strUrl) throws IOException {
  String data = "";
  HttpURLConnection urlConnection;
  URL url = new URL(strUrl);
  
  // Creating an http connection to communicate with url
  urlConnection = (HttpURLConnection) url.openConnection();
  
  // Connecting to url
  urlConnection.connect();
  
  // Reading data from url
  try (InputStream iStream = urlConnection.getInputStream()) {
      BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
  
      StringBuilder sb = new StringBuilder();
  
      String line;
      while ((line = br.readLine()) != null) {
          sb.append(line);
      }
  
      data = sb.toString();
  
      br.close();
  
  } catch (Exception e) {
      Log.d("Excp. while downloading", e.toString());
  } finally {
      urlConnection.disconnect();
  }
  return data; }
  
  interface taskCompleteListener {
  void taskCompleted(String[] time_distance); }
  
  private class DownloadTask extends AsyncTask&lt;String, Void, String&gt; {
  
  // Downloading data in non-ui thread
  @Override
  protected String doInBackground(String... url) {
  
      // For storing data from web service
      String data = "";
  
      try {
          // Fetching the data from web service
          data = downloadUrl(url[0]);
      } catch (Exception e) {
          Log.d("Background Task", e.toString());
      }
      return data;
  }
  
  // Executes in UI thread, after the execution of
  // doInBackground()
  @Override
  protected void onPostExecute(String result) {
      super.onPostExecute(result);
  
      ParserTask parserTask = new ParserTask();
  
      // Invokes the thread for parsing the JSON data
      parserTask.execute(result);
  
  } }
  
  private class ParserTask extends AsyncTask&lt;String, Integer,
  List&lt;HashMap&lt;String, String&gt;&gt;&gt; {
  
  // Parsing the data in non-ui thread
  @Override
  protected List&lt;HashMap&lt;String, String&gt;&gt; doInBackground(String... jsonData) {
  
      JSONObject jObject;
      List&lt;HashMap&lt;String, String&gt;&gt; routes = null;
  
      try {
          jObject = new JSONObject(jsonData[0]);
          DistanceTimeParser parser = new DistanceTimeParser();
  
          // Starts parsing data
          routes = parser.parse(jObject);
      } catch (Exception e) {
          e.printStackTrace();
      }
      return routes;
  }
  
  // Executes in UI thread, after the parsing process
  @Override
  protected void onPostExecute(List&lt;HashMap&lt;String, String&gt;&gt; result) {
  
      String duration_distance = "";
  
  
      if (result.size() &lt; 1) {
          Log.e("Error : ", "No Points found");
          return;
      }
  
  
      String[] date_dist = new String[2];
  
      // Traversing through all the routes
      for (int i = 0; i &lt; result.size(); i++) {
  
          // Fetching i-th route
          HashMap&lt;String, String&gt; tmpData = result.get(i);
          Set&lt;String&gt; key = tmpData.keySet();
          Iterator it = key.iterator();
          while (it.hasNext()) {
              String hmKey = (String) it.next();
              duration_distance = tmpData.get(hmKey);
  
              System.out.println("Key: " + hmKey + " &amp; Data: " + duration_distance);
  
              it.remove(); // avoids a ConcurrentModificationException
          }
  
          date_dist[i] = duration_distance;
      }
  
      mTaskListener.taskCompleted(date_dist);
  }   } }
  </code></pre>
  </blockquote>
  
  <p>DistanceTimeParser.java (added new class)</p>
  
  <blockquote>
  <pre><code>public class DistanceTimeParser {
  public List&lt;HashMap&lt;String, String&gt;&gt; parse(JSONObject jObject) {
  
  
      List&lt;HashMap&lt;String, String&gt;&gt; routes = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
      JSONArray jRoutes = null;
      JSONArray jLegs = null;
  
      JSONObject jDistance = null;
      JSONObject jDuration = null;
  
      try {
  
          jRoutes = jObject.getJSONArray("routes");
  
          jLegs = ((JSONObject) jRoutes.get(0)).getJSONArray("legs");
  
          List&lt;HashMap&lt;String, String&gt;&gt; path = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
  
          /** Getting distance from the json data */
          jDistance = ((JSONObject) jLegs.get(0)).getJSONObject("distance");
          HashMap&lt;String, String&gt; hmDistance = new HashMap&lt;String, String&gt;();
          hmDistance.put("distance", jDistance.getString("text"));
  
          /** Getting duration from the json data */
          jDuration = ((JSONObject) jLegs.get(0)).getJSONObject("duration");
          HashMap&lt;String, String&gt; hmDuration = new HashMap&lt;String, String&gt;();
          hmDuration.put("duration", jDuration.getString("text"));
  
          routes.add(hmDistance);
  
          routes.add(hmDuration);
  
      } catch (JSONException e) {
          e.printStackTrace();
      } catch (Exception e) {
      }
  
      return routes;
  } }
  </code></pre>
  </blockquote>
  
  <p>DirectionsJSONParser.java (added new class)</p>
  
  <blockquote>
  <pre><code>public class DirectionsJSONParser {
  
  List&lt;List&lt;HashMap&lt;String,String&gt;&gt;&gt; parse(JSONObject jObject){
  
      List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt; routes = new ArrayList&lt;&gt;() ;
      JSONArray jRoutes ;
      JSONArray jLegs ;
      JSONArray jSteps ;
  
      try {
  
          jRoutes = jObject.getJSONArray("routes");
  
  
          for(int i=0;i&lt;jRoutes.length();i++){
              jLegs = ( (JSONObject)jRoutes.get(i)).getJSONArray("legs");
              List path = new ArrayList&lt;&gt;();
  
              for(int j=0;j&lt;jLegs.length();j++){
                  jSteps = ( (JSONObject)jLegs.get(j)).getJSONArray("steps");
  
  
                  for(int k=0;k&lt;jSteps.length();k++){
                      String polyline;
                      polyline = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("polyline")).get("points");
                      List&lt;LatLng&gt; list = decodePoly(polyline);
  
  
                      for(int l=0;l&lt;list.size();l++){
                          HashMap&lt;String, String&gt; hm = new HashMap&lt;&gt;();
                          hm.put("lat", Double.toString((list.get(l)).latitude) );
                          hm.put("lng", Double.toString((list.get(l)).longitude) );
                          path.add(hm);
                      }
                  }
                  routes.add(path);
              }
          }
  
      } catch (JSONException e) {
          e.printStackTrace();
      }catch (Exception ignored){
      }
  
      return routes;
  }
  
  private List&lt;LatLng&gt; decodePoly(String encoded) {
  
      List&lt;LatLng&gt; poly = new ArrayList&lt;&gt;();
      int index = 0, len = encoded.length();
      int lat = 0, lng = 0;
  
      while (index &lt; len) {
          int b, shift = 0, result = 0;
          do {
              b = encoded.charAt(index++) - 63;
              result |= (b &amp; 0x1f) &lt;&lt; shift;
              shift += 5;
          } while (b &gt;= 0x20);
          int dlat = ((result &amp; 1) != 0 ? ~(result &gt;&gt; 1) : (result &gt;&gt; 1));
          lat += dlat;
  
          shift = 0;
          result = 0;
          do {
              b = encoded.charAt(index++) - 63;
              result |= (b &amp; 0x1f) &lt;&lt; shift;
              shift += 5;
          } while (b &gt;= 0x20);
          int dlng = ((result &amp; 1) != 0 ? ~(result &gt;&gt; 1) : (result &gt;&gt; 1));
          lng += dlng;
  
          LatLng p = new LatLng((((double) lat / 1E5)),
                  (((double) lng / 1E5)));
          poly.add(p);
      }
  
      return poly;
  } }
  </code></pre>
  </blockquote>
  
  <p>These 4 methods added in mapsActivity.java</p>
  
  <blockquote>
  <pre><code>  private String getDirectionsUrl(LatLng origin, LatLng dest) {
  
      String str_origin = "origin=" + origin.latitude + "," + origin.longitude;
  
      String str_dest = "destination=" + dest.latitude + "," + dest.longitude;
  
      String sensor = "sensor=false";
  
      String parameters = str_origin + "&amp;" + str_dest + "&amp;" + sensor;
  
      String output = "json";
  
      return "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;
  }
  
    @SuppressLint("NewApi")
    public static String downloadUrl(String strUrl) throws IOException {
      String data = "";
      HttpURLConnection urlConnection;
      URL url = new URL(strUrl);
  
      // Creating an http connection to communicate with url
      urlConnection = (HttpURLConnection) url.openConnection();
  
      // Connecting to url
      urlConnection.connect();
  
      // Reading data from url
      try (InputStream iStream = urlConnection.getInputStream()) {
          BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
  
          StringBuilder sb = new StringBuilder();
  
          String line;
          while ((line = br.readLine()) != null) {
              sb.append(line);
          }
  
          data = sb.toString();
  
          br.close();
  
      } catch (Exception e) {
          Log.d("Exception while down", e.toString());
      } finally {
          urlConnection.disconnect();
      }
      return data;
  }
  
    private class DownloadTask extends AsyncTask&lt;String, Void, String&gt; {
  
      // Downloading data in non-ui thread
      @Override
      protected String doInBackground(String... url) {
  
          // For storing data from web service
          String data = "";
  
          try {
              // Fetching the data from web service
              data = downloadUrl(url[0]);
          } catch (Exception e) {
              Log.d("Background Task", e.toString());
          }
          return data;
      }
  
      // Executes in UI thread, after the execution of
      // doInBackground()
      @Override
      protected void onPostExecute(String result) {
          super.onPostExecute(result);
  
          ParserTask parserTask = new ParserTask();
  
          // Invokes the thread for parsing the JSON data
          parserTask.execute(result);
      }
  }
  
  private class ParserTask extends AsyncTask&lt;String, Integer, List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt;&gt; {
  
      @Override
      protected List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt; doInBackground(String... jsonData) {
  
          JSONObject jObject;
          List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt; routes = null;
  
          try {
              jObject = new JSONObject(jsonData[0]);
              DirectionsJSONParser parser = new DirectionsJSONParser();
  
              // Starts parsing data
              routes = parser.parse(jObject);
          } catch (Exception e) {
              e.printStackTrace();
          }
          return routes;
      }
  
      @Override
      protected void onPostExecute(List&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt; result) {
          ArrayList&lt;LatLng&gt; points = new ArrayList&lt;&gt;();
          PolylineOptions lineOptions = new PolylineOptions();
          for (int i = 0; i &lt; result.size(); i++) {
              List&lt;HashMap&lt;String, String&gt;&gt; path = result.get(i);
              for (int j = 0; j &lt; path.size(); j++) {
                  HashMap&lt;String, String&gt; point = path.get(j);
                  double lat = Double.parseDouble(point.get("lat"));
                  double lng = Double.parseDouble(point.get("lng"));
                  LatLng position = new LatLng(lat, lng);
                  points.add(position);
              }
              lineOptions.addAll(points);
              lineOptions.width(10);
              lineOptions.color(Color.WHITE);
              mMap.addPolyline(lineOptions);
          }
      }
  }
  </code></pre>
  </blockquote>
  
  <p>mapsActivity.java (my current location in map)</p>
  
  <blockquote>
  <pre><code>@Override
  public void onLocationChanged(Location location) {
      Log.d("onLocationChanged", "entered");
  
      mLastLocation = location;
  
      DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
      Date date = new Date();
      mLastUpdateTime = ((dateFormat.format(date).toString()));
  
      saveToFirebase();
  
      if (mCurrLocationMarker != null) {
          mCurrLocationMarker.remove();
      }
  
      latitude = location.getLatitude();
      longitude = location.getLongitude();
  
      LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
      MarkerOptions markerOptions = new MarkerOptions();
      markerOptions.position(latLng);
      markerOptions.draggable(false);
      markerOptions.title("Current Position");
      markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
      mCurrLocationMarker = mMap.addMarker(markerOptions);
  
      //move map camera
      mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
      mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
  
      Toast.makeText(MapsActivity.this, "Your Current Location", Toast.LENGTH_LONG).show();
  
      //stop location updates
      if (mGoogleApiClient != null) {
       LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,
     this);
          Log.d("onLocationChanged", "Removing Location Updates");
      }
  }
  </code></pre>
  </blockquote>
  
  <p>Data Saved in Fire Base Method</p>
  
  <blockquote>
  <pre><code>public void saveToFirebase() {
  
      DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Email").push();
      String email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
      UserInformation userInformation = new UserInformation(email, mLastLocation.getLatitude(), mLastLocation.getLongitude());
      ref.setValue(userInformation);
  }
  </code></pre>
  </blockquote>