如何根据速度更改折线颜色

时间:2014-01-11 02:57:15

标签: java android google-maps google-maps-api-2 google-polyline

我正在尝试制作一个应用程序,我根据速度为折线设置了3种颜色,但是他们的折线没有显示。我创建了一个函数并将其添加到折线但是没有显示的线条起来。 任何人都可以帮助我我的代码如下::。 谢谢

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

1 个答案:

答案 0 :(得分:0)

我不确定这是否正确但似乎您需要更改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 );
    }
}
}