Use different color for path segment in the map view overlay
<p>My application can give a path/direction using JSON. How can I change in different colors in on path given by JSON?</p>

<p>For example, form point A to point B is red, B to C is yellow, C to D is green, and so on. </p>

<p>The code is: </p>

<p>Getting points in JSON.</p>

<pre><code>    private void parsing(GeoPoint start, GeoPoint end) throws ClientProtocolException, IOException, JSONException, URISyntaxException{
    HttpClient httpclient = new DefaultHttpClient();
    StringBuilder urlstring = new StringBuilder();
    urlstring.append("https://maps.googleapis.com/maps/api/directions/json?origin=")
    .append(Double.toString((double)start.getLatitudeE6()/1E6)).append(",").append(Double.toString((double)start.getLongitudeE6()/1E6)).append("&amp;destination=")
    .append(Double.toString((double)end.getLatitudeE6()/1E6)).append(",").append(Double.toString((double)end.getLongitudeE6()/1E6))
    .append("&amp;sensor=false");
    //urlstring.append("http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&amp;destination=Montreal&amp;sensor=true");
    url = new URI(urlstring.toString());

    HttpPost httppost = new HttpPost(url);

    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    InputStream is = null;
    is = entity.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    sb.append(reader.readLine() + "\n");
    String line = "0";
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();
    reader.close();
    String result = sb.toString();
    JSONObject jsonObject = new JSONObject(result);
    JSONArray routeArray = jsonObject.getJSONArray("routes");
    JSONObject routes = routeArray.getJSONObject(0);
    JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
    String encodedString = overviewPolylines.getString("points");
    List&lt;GeoPoint&gt; pointToDraw = decodePoly(encodedString);

    //Added line:
    mv_mapview.getOverlays().add(new RoutePathOverlay(pointToDraw));
}
</code></pre>

<p>List of Geopoints given by JSON.</p>

<pre><code>    private List&lt;GeoPoint&gt; decodePoly(String encoded) {

    List&lt;GeoPoint&gt; poly = new ArrayList&lt;GeoPoint&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;



        GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6), (int) (((double) lng / 1E5) * 1E6));

        //n = new Node(p,);

        poly.add(p);


    }

    return poly;
}
</code></pre>

<p>and Overlay class</p>

<pre><code>    public class RoutePathOverlay extends Overlay {

private int _pathColor;
private final List&lt;GeoPoint&gt; _points;
private boolean _drawStartEnd;

public RoutePathOverlay(List&lt;GeoPoint&gt; points) {


    this(points, Color.GREEN, true);
}


public RoutePathOverlay(List&lt;GeoPoint&gt; points, int pathColor, boolean drawStartEnd) {
        _points = points;
        _pathColor = pathColor;
        _drawStartEnd = drawStartEnd;
}


private void drawOval(Canvas canvas, Paint paint, Point point) {
        Paint ovalPaint = new Paint(paint);
        ovalPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        ovalPaint.setStrokeWidth(2);
        int _radius = 6;
        RectF oval = new RectF(point.x - _radius, point.y - _radius, point.x + _radius, point.y + _radius);
        canvas.drawOval(oval, ovalPaint);               
}

public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
        Projection projection = mapView.getProjection();
        if (shadow == false &amp;&amp; _points != null) {
                Point startPoint = null, endPoint = null;
                Path path = new Path();
                //We are creating the path
                for (int i = 0; i &lt; _points.size(); i++) {
                        GeoPoint gPointA = _points.get(i);
                        Point pointA = new Point();
                        projection.toPixels(gPointA, pointA);
                       //if() 
                        if (i == 0) { //This is the start point
                                startPoint = pointA;
                                path.moveTo(pointA.x, pointA.y);
                        } else {
                                if (i == _points.size() - 1)//This is the end point
                                        endPoint = pointA;
                                path.lineTo(pointA.x, pointA.y);
                        }
                }

                Paint paint = new Paint();
                paint.setAntiAlias(true);
               // if(){
                paint.setColor(_pathColor);
               //}
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(5);
                paint.setAlpha(90);
                if (getDrawStartEnd()) {
                        if (startPoint != null) {
                                drawOval(canvas, paint, startPoint);
                        }
                        if (endPoint != null) {
                                drawOval(canvas, paint, endPoint);
                        }
                }
                if (!path.isEmpty())
                        canvas.drawPath(path, paint);
        }
        return super.draw(canvas, mapView, shadow, when);
}

public boolean getDrawStartEnd() {
        return _drawStartEnd;
}

public void setDrawStartEnd(boolean markStartEnd) {
        _drawStartEnd = markStartEnd;
}
</code></pre>

<p>}</p>
