I'm getting Latitudes and Longitudes from Google Directions API and using them to show polyline between two locations. Polyline is being drawn but not following the exact road or path that it must follow.
Here are my screenshots-
Here is my code through which i'm getting LatLngs and displaying polyline.
public void requestDirections(final String mode, String origin, String destination)
{
final ProgressDialog progress=new ProgressDialog(this);
progress.setMessage("Finding route");
progress.show();
Retrofit retrofit= new Retrofit.Builder().baseUrl("https://maps.googleapis.com/").addConverterFactory(GsonConverterFactory.create()).build();
Call<DirectionResponse> call=retrofit.create(ApiDirections.class).getDirections(mode, origin, destination);
call.enqueue(new Callback<DirectionResponse>() {
@Override
public void onResponse(Call<DirectionResponse> call, Response<DirectionResponse> response) {
for(int i=0; i<response.body().getRoutes().get(0).getLegs().get(0).getSteps().size(); i++)
{
LatLng startLoc=new LatLng(response.body().getRoutes().get(0).getLegs().get(0).getSteps().get(i).getStartLocation().getLat(), response.body().getRoutes().get(0).getLegs().get(0).getSteps().get(i).getStartLocation().getLng());
LatLng endLoc=new LatLng(response.body().getRoutes().get(0).getLegs().get(0).getSteps().get(i).getEndLocation().getLat(), response.body().getRoutes().get(0).getLegs().get(0).getSteps().get(i).getEndLocation().getLng());
Polyline polyline=response.body().getRoutes().get(0).getLegs().get(0).getSteps().get(i).getPolyline();
DataHandler.startingLocations.add(startLoc);
DataHandler.endingLocations.add(endLoc);
System.out.println("Starting Loc "+i+" "+startLoc);
System.out.println("Ending Loc "+i+" "+endLoc);
System.out.println("polyline "+i+" "+polyline.getPoints());
}
LatLng endLocation=new LatLng(response.body().getRoutes().get(0).getLegs().get(0).getEndLocation().getLat(), response.body().getRoutes().get(0).getLegs().get(0).getEndLocation().getLng());
marker=mMap.addMarker(new MarkerOptions().position(endLocation).flat(true));
marker.setTag(destinationName);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(DataHandler.startingLocations.get((response.body().getRoutes().get(0).getLegs().get(0).getSteps().size())/2), 10));
polylineOptions=new PolylineOptions();
polylineOptions.addAll(DataHandler.endingLocations);
polylineOptions.color(getResources().getColor(R.color.colorAccent));
polylineOptions.width(12);
polylineOptions.startCap(new RoundCap());
polylineOptions.endCap(new RoundCap());
mMap.addPolyline(polylineOptions);
textViewJourneyDistance.setText(response.body().getRoutes().get(0).getLegs().get(0).getDistance().getText());
textViewJourneyTime.setText(response.body().getRoutes().get(0).getLegs().get(0).getDuration().getText());
progress.dismiss();
}
@Override
public void onFailure(Call<DirectionResponse> call, Throwable t) {
progress.dismiss();
}
});
}
public interface ApiDirections{
@GET("maps/api/directions/json?key=My_API_KEY")
Call<DirectionResponse> getDirections(@Query("mode") String mode,
@Query("origin") String origin,
@Query("destination") String destination);
}
you need to get routelist
ArrayList<LatLng> routelist = new ArrayList<LatLng>();
if (response.body().getRoutes().size() > 0) {
ArrayList<LatLng> decodelist;
Route routeA = response.body().getRoutes().get(0);
Log.e("ad", "Legs length : " + routeA.getLegs().size());
if (routeA.getLegs().size() > 0) {
List<Steps> steps = routeA.getLegs().get(0).getSteps();
Log.e("ad", "Steps size :" + steps.size());
Steps step;
Location location;
String polyline;
for (int i = 0; i < steps.size(); i++) {
step = steps.get(i);
location = step.getStart_location();
routelist.add(new LatLng(location.getLat(), location.getLng()));
Log.e("ad", "Start Location :" + location.getLat() + ", " + location.getLng());
polyline = step.getPolyline().getPoints();
decodelist = RouteDecode.decodePoly(polyline);
routelist.addAll(decodelist);
location = step.getEnd_location();
routelist.add(new LatLng(location.getLat(), location.getLng()));
Log.e("ad", "End Location :" + location.getLat() + ", " + location.getLng());
Integer disInMeters = routeA.getLegs().get(0).getDistance().getValue();
int kilometers = (int) (disInMeters * 0.001);
}
}
}
if (routelist.size() > 0) {
PolylineOptions rectLine = new PolylineOptions().width(10).color(ContextCompat.getColor(SendSomethingTodayActivityThree.this, R.color.colorPrimary));
for (int i = 0; i < routelist.size(); i++) {
rectLine.add(routelist.get(i));
}}
its work for me