我从Google Directions API获取纬度和经度,并使用它们显示两个位置之间的折线。正在绘制折线但不遵循它必须遵循的确切道路或路径。
以下是我的截图 -
这是我的代码,通过它我可以获取LatLng并显示折线。
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);
}
答案 0 :(得分:1)
您需要获得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));
}}
它为我工作