您好我在Google地图上创建了多个路点的路径。我想为每个路点添加不同的颜色。我尝试使用多线但不起作用。它只显示一种颜色。
请在下面找到代码:
@Override
protected void onPostExecute(List<List<HashMap<String, String>>> routes) {
ArrayList<LatLng> points;
try {
for (int i = 0; i < routes.size(); i++) {
points = new ArrayList<>();
PolylineOptions polyLineOptions = new PolylineOptions();
List<HashMap<String, String>> path = routes.get(i);
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
try {
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
polyLineOptions.addAll(points);
polyLineOptions.width(4);
int color = getRandomColor();
polyLineOptions.color(color);
if (polyline != null) {
polyline.remove();
}
polyline = mMap.addPolyline(polyLineOptions);
}
} catch (NullPointerException e) {
e.printStackTrace();
}
progressBar.setVisibility(View.INVISIBLE);
}
}
public int getRandomColor() {
Random rnd = new Random();
return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
}