Hi, I was trying to draw the polygon using Direction API in android application in google map. I am getting all the values perfectly but I was unable to view the polygon in map. Below is my code
String url = "https://graphhopper.com/api/1/route?point=12.728030,77.827057&point=13.082680,80.270721&vehicle=&debug=true&key=&type=json";
StringRequest request = new StringRequest(url, new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String s) {
Log.i("dataaa ",s);
try {
if (client != null){
MarkerOptions marker = new MarkerOptions()
.position(client)
.title("Client Location")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
map.addMarker(marker);
LatLngBounds.Builder latLngBuilder = new LatLngBounds.Builder();
latLngBuilder.include(client);
// if (myLocation != null){
// latLngBuilder.include(myLocation);
// }
LatLngBounds latLngBounds = latLngBuilder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds,45));
}
dialog.dismiss();
JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("paths");
JSONObject job = jsonArray.getJSONObject(0);
// JSONObject points = job.getJSONObject("overview_polyline");
List<LatLng> latLngs = decodePoly(job.getString("points"));
PolylineOptions polylineOptions = new PolylineOptions().
geodesic(true).
color(Color.parseColor("#4CAF50")).
width(10);
for (int i = 0; i < latLngs.size(); i++){
polylineOptions.add(latLngs.get(i));
Log.i("dataaaa ",polylineOptions.add(latLngs.get(i)) +"lines ");
}
List<Polyline> polylinePaths = new ArrayList<>();
polylinePaths.add(map.addPolyline(polylineOptions));
Method using for decoding Polyline
private List<LatLng> decodePoly(String encoded) {
List<LatLng> poly = new ArrayList<LatLng>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
LatLng p = new LatLng((((double) lat / 1E5)),
(((double) lng / 1E5)));
poly.add(p);
}
return poly;
}
Please check this and help me to solve.