在地图上显示具有不同颜色的多条折线

时间:2016-06-17 15:35:22

标签: android google-maps polyline

我正在使用此代码在Google地图上显示行车路线。问题在于,每当我想要添加多个不同 颜色的折线时,我得到的颜色相同的所有折线都是所用for循环中的最后一个。< / p>

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包含BLACK和GREEN,但是当显示路线时,我得到它们的颜色相对于GREEN(我尝试了多个组合,我总是得到最后一个循环的颜色)。 任何想法如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我正面临同样的问题,我有多条路径在其中有共同区域。我试图使所选路径为红色而其他所有灰色但在两条多边形线的公共区域我得到了颜色最后绘制的多边形线。我通过删除多边形线并重新绘制它来解决问题。并且需要在顶部显示的多边形线,最后绘制。

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);
}

希望它有所帮助。