Java 如何根据速度更改多段线颜色
我正在尝试制作一个应用程序,其中我根据速度为多段线设置了3种颜色,但没有显示多段线。我创建了一个函数并将其添加到多段线,但没有显示任何线。 有人能帮我写下面的代码吗。 多谢各位Java 如何根据速度更改多段线颜色,java,android,google-maps,google-maps-api-2,google-polyline,Java,Android,Google Maps,Google Maps Api 2,Google Polyline,我正在尝试制作一个应用程序,其中我根据速度为多段线设置了3种颜色,但没有显示多段线。我创建了一个函数并将其添加到多段线,但没有显示任何线。 有人能帮我写下面的代码吗。 多谢各位 PolylineOptions rectOptions = new PolylineOptions(); @Override public void onLocationChanged(Location location) { // mMessageView.setText("Location = "
PolylineOptions rectOptions = new PolylineOptions();
@Override
public void onLocationChanged(Location location) {
// mMessageView.setText("Location = " + location);
rectOptions.add(new LatLng(location.getLatitude(), location.getLongitude()));
if (setIt == true){
setcolortopoly();
mMap.addPolyline(rectOptions);
}
}
public void setcolortopoly(){
if(mIsMetric){
if(speed_poly<5.0){
rectOptions = new PolylineOptions().width(3).color(
Color.YELLOW );
}
else if(5.0<= speed_poly && speed_poly <10){
rectOptions = new PolylineOptions().width(3).color(
Color.GREEN );
}
else if(10.0<=speed_poly){
rectOptions = new PolylineOptions().width(3).color(
Color.RED );
}
}
else{
if(speed_poly<3.1){
rectOptions = new PolylineOptions().width(3).color(
Color.YELLOW );
}
else if(3.1<= speed_poly && speed_poly <6.21){
rectOptions = new PolylineOptions().width(3).color(
Color.GREEN );
}
else if(6.21<=speed_poly){
rectOptions = new PolylineOptions().width(3).color(
Color.RED );
}
}
}
我不确定这是否正确,但似乎您需要更改if循环的条件,请检查下面的代码是否可以进行一些更改:
PolylineOptions rectOptions = new PolylineOptions();
@Override
public void onLocationChanged(Location location) {
// mMessageView.setText("Location = " + location);
rectOptions.add(new LatLng(location.getLatitude(), location.getLongitude()));
if (setIt == true){
setcolortopoly();
mMap.addPolyline(rectOptions);
}
}
public void setcolortopoly(){
if(mIsMetric){
if(speed_poly<5.0){
rectOptions = new PolylineOptions().width(3).color(
Color.YELLOW );
}
else if(5.0>= speed_poly && speed_poly <10){
rectOptions = new PolylineOptions().width(3).color(
Color.GREEN );
}
else if(10.0>=speed_poly){
rectOptions = new PolylineOptions().width(3).color(
Color.RED );
}
}
else{
if(speed_poly<3.1){
rectOptions = new PolylineOptions().width(3).color(
Color.YELLOW );
}
else if(3.1>=speed_poly && speed_poly <6.21){
rectOptions = new PolylineOptions().width(3).color(
Color.GREEN );
}
else if(6.21>=speed_poly){
rectOptions = new PolylineOptions().width(3).color(
Color.RED );
}
}
}