I use SupportMapFragment to show google map. I use Direction API two route between two location. I parse JSON data that returned form Direction API with GSON library and extract steps tag that show each route. then I use polyline to draw it. but my route doesn't match on road and mostly it is straight line.
this code is my extraction of JSON Direction that google return:
GoogleDirectionResponse data = (GoogleDirectionResponse) response;
if (data.getStatus().matches("OK")){
List<LatLng> steps = new ArrayList<>();
RoutesData routesData = data.getRoutes().get(0);
LegData legData = routesData.getLegs().get(0);
for (StepsData item : legData.getSteps()){
LatLng start = new LatLng(item.getStart_location().getLat(), item.getStart_location().getLng());
LatLng end = new LatLng(item.getEnd_location().getLat(), item.getEnd_location().getLng());
steps.add(start);
steps.add(end);
}
onDirectionFetchedListener.onDirectionFetched(steps);
}
I hold LatLng data in array and pass it to fragment to draw it:
@Override
public void onDirectionFetched(List<LatLng> steps) {
PolylineOptions rectLine = new PolylineOptions().width(3).color(
Color.RED);
for (LatLng item : steps) {
rectLine.add(item);
}
Polyline polylin = map.addPolyline(rectLine);
}
and this is my map:
take look at red line. that is not go on road. how can fix this issue. thanks