Reputation: 197
I'm using this code to display a driving route on a google map. the problem is that whenever i want to add multiple polylines with different colors, i get all the polylines with the same color which is the last one in the for loop used.
public void requestDirection(final LatLng nUserCurrentLoc, final LatLng objectiveLatLng) {
Toast.makeText(getBaseContext(), "Requesting direction, just a moment...", Toast.LENGTH_SHORT).show();
GoogleDirection.withServerKey(SERVER_API_KEY)
.from(nUserCurrentLoc)
.to(objectiveLatLng)
.transportMode(TransportMode.DRIVING)
.execute(this);
uiHandler.postDelayed(new Runnable() {
@Override
public void run() {
positionCamera(nUserCurrentLoc, objectiveLatLng);
}
}, 2800);
}
@Override
public void onDirectionSuccess(Direction direction, String rawBody) {
if (direction.isOK()) {
Toast.makeText(getBaseContext(), "Routes are marked successfully...", Toast.LENGTH_SHORT).show();
final ArrayList<LatLng> directionPositionList = direction.getRouteList().get(0).getLegList().get(0).getDirectionPoint();
int zzzz = -16711681;
for(int i =0; i<colorList.size(); i++){
String pls = (String)colorList.get(i);
if (pls.equals("BLUE")) zzzz = -16711681;
else if (pls.equals("RED")) zzzz = -65281;
else if (pls.equals("GREEN")) zzzz = -256;
else if (pls.equals("BLACK")) zzzz = -7829368;
else if (pls.equals("DKGRAY")) zzzz = -3355444;
itineraryLines = mMap.addPolyline(DirectionConverter.createPolyline(getApplicationContext(),
directionPositionList, 5, zzzz));
}
}else {
Toast.makeText(getBaseContext(), "Routes are suspicious!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onDirectionFailure(Throwable t) {
Toast.makeText(getBaseContext(), "No routes found!", Toast.LENGTH_SHORT).show();
}
colorList contains BLACK and GREEN, but when the routes are displayed i get them both with the color relative to GREEN ( i tried multiple combination, i always get the last loop's color). Any ideas how to solve this ?
Upvotes: 1
Views: 1003
Reputation: 1335
well i was facing the same issue,i had multiple paths having common region in them.what i was trying to make the chosen path red color and all else grey but in the common region of two poly lines i was getting the color of last drawn poly line.I got my solution working by removing the poly lines and redrawing them .And the poly line that needed to show at top ,drawn at last.
public void getList(Direction direction,int a)
{
PolylineOptions abc=new PolylineOptions();
ArrayList<LatLng> directionPositionList=new ArrayList<>();
if(lines.size()>0)
{
for(int i=0;i<lines.size();i++)
{
lines.get(i).remove();
}
//map.clear();
lines.clear();
line=null;
}
for (int i = 0; i < direction.getRouteList().size(); i++)
{
line = null;
for (int j = 0; j < direction.getRouteList().get(i).getLegList().size(); j++)
{directionPositionList = direction.getRouteList().get(i).getLegList().get(j).getDirectionPoint();}
if (i != a)
{
line = map.addPolyline(DirectionConverter.createPolyline(getContext(), directionPositionList, 5, Color.GRAY));
lines.add(line);
line.setClickable(true);
}
if(i==a)
{
line = map.addPolyline(DirectionConverter.createPolyline(getContext(), directionPositionList, 5, Color.TRANSPARENT));
test=directionPositionList;
lines.add(line);
line.remove();
index=i;
}
}
line = map.addPolyline(DirectionConverter.createPolyline(getContext(), test, 5, Color.RED));
lines.set(index,line);
line.setClickable(true);
}
Hope it helps.
Upvotes: 1