Android谷歌地图备用路线和最短路径

时间:2017-02-27 02:59:51

标签: android google-maps

我已经搜遍了所有人,但我找不到答案。 Similar to this 如何将颜色更改为备用路线? urlDestination +"&alternatives=true" 添加该代码将显示最短路径和备用路由。问题是,我不知道如何将备用路线的颜色更改为特定颜色。 示例:最短路径应为蓝色,备用路径应为灰色。 非常需要帮助。

Something like this... that the alternate routes should be grey

2 个答案:

答案 0 :(得分:0)

选中此Tutorial

您需要添加BelowLine。

polyLineOptions.color(Color.BLUE);

希望这可能会有所帮助。

答案 1 :(得分:0)

这很直截了当。我相信你有一个列表或路由数组。 通过这个迭代来找到最小距离指数。 (通常是第一个,但只是为了确保)。

int minDistanceIndex = 0;
int minDistance = Integer.MAX_VALUE;
for(int i = 0; i < routes.size(); i++){
    Route route = routes.get(i);
    int distance = route.getDistanceValue();
    if(distance < minDistance){
       minDistance = distance;
       minDistanceIndex = i;
    }
}

现在使用minDistanceIndex显示默认路径(蓝色),将其他路由显示为灰色,如下所示。

PolylineOptions lineOptions;
for (int i = 0; i < routes.size(); i++) {
    points = routes.get(i).getPoints();
    lineOptions = new PolylineOptions();
    // Adding all the points in the route to LineOptions
    lineOptions.addAll(points);
    if(minDistanceIndex != i) {
        lineOptions.width(15);
        lineOptions.color(ContextCompat.getColor(getActivity(), android.R.color.darker_gray));
    }
    lineOptions.clickable(true);
    // Drawing polyline in the Google Map for the i-th route
    if(map != null) {
        polylines.add(map.addPolyline(lineOptions));
        map.setOnPolylineClickListener(polylineListener);
    }
}
//finally draw the shortest route
lineOptions = new PolylineOptions();
lineOptions.width(18);
lineOptions.color(ContextCompat.getColor(getActivity(), android.R.color.holo_blue_dark));
// Drawing polyline in the Google Map for the i-th route
if(map != null) {
    polylines.add(map.addPolyline(lineOptions));
}

您可以在折线上添加点击侦听器,以便在用户选择其他折线时更改颜色,就像在Google地图中一样。

IMP:您需要在最后一个时间绘制最短路线,否则该路线的一部分可能会被替代路线重叠,因此会留下部分蓝色和部分灰色路线。

希望这有帮助!