在nMap android studio中确定点颜色

时间:2018-05-07 08:37:28

标签: java android-studio

我在android工作室中创建了一个mMap标记项目,其中数据使用json ...连接到数据库。我想问一下,如何指定颜色标记" if else"使用以下术语:

ken ken carane ??

enter image description here 1

这是我的代码:

//GENERATE DATA MAP
            //Toast.makeText(getApplicationContext(), "Jumlah SIte Id X = "+SITE_ID.size(), Toast.LENGTH_LONG).show();
            for(int i = 0 ; i < SITE_ID.size() ; i++){

                Double lat = Double.parseDouble(LAT.get(i));
                Double longi = Double.parseDouble(LONG.get(i));

            BitmapDescriptor revicon = null;
              double Rev = Double.parseDouble(""+Rev_ALL_month3.get(i)) ;

                if( Rev >= 200000000) {
                    revicon = BitmapDescriptorFactory
                            .fromResource(R.drawable.merah);
                }
                    else if(Rev >= 100000000 && Rev <= 200000000) {
                    revicon = BitmapDescriptorFactory
                            .fromResource(R.drawable.kuning);
                }
                          else if(Rev >=60000000 && Rev <= 100000000) {
                    revicon = BitmapDescriptorFactory
                            .fromResource(R.drawable.biru);
                }
                                  else if(Rev >=30000000 && Rev <= 60000000) {
                    revicon = BitmapDescriptorFactory
                            .fromResource(R.drawable.hijau);
                }
                                      else if(Rev >= 30000000) {
                    revicon = BitmapDescriptorFactory
                            .fromResource(R.drawable.hitam);
                }



                mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(lat,longi))
                        .title(SITE_ID.get(i))
                        .snippet("Data Handset 2G :" +"Des 2017("+Jml_Handset_2G_month1.get(i)+"), Jan 2018("+Jml_Handset_2G_month2.get(i)+"), Feb 2018("+Jml_Handset_2G_month3.get(i)+"), MoM("+Jml_Handset_2G_inc.get(i)+")\n\n"
                                +"Data Handset 3G :" +"Des 2018("+Jml_Handset_3G_month1.get(i)+"), Jan 2018("+Jml_Handset_3G_month2.get(i)+"), Feb 2018("+Jml_Handset_3G_month3.get(i)+"), MoM("+Jml_Handset_3G_inc.get(i)+")\n\n"
                                +"Data Handset 4G :" +"Des 2017("+Jml_Handset_4G_month1.get(i)+"), Jan 2018("+Jml_Handset_4G_month2.get(i)+"), Feb 2018("+Jml_Handset_4G_month3.get(i)+"), MoM("+Jml_Handset_4G_inc.get(i)+")\n\n"


                                +"Revenue Voice :" +"Des 2017("+Rev_Voice_month1.get(i)+"), Jan 2018("+Rev_Voice_month2.get(i)+"), Feb 2018("+Rev_Voice_month3.get(i)+"), MoM("+Rev_Voice_inc.get(i)+")\n\n"
                                +"Revenue SMS :" +"Des 2017("+Rev_SMS_month1.get(i)+"), Jan 2018("+Rev_SMS_month2.get(i)+"), Feb 2018("+Rev_SMS_month3.get(i)+"), MoM("+Rev_SMS_inc.get(i)+")\n\n"
                                +"Revenue Broadband :" +"Des 2017("+Rev_BBand_month1.get(i)+"), Jan 2018("+Rev_BBand_month2.get(i)+"), Feb 2018("+Rev_BBand_month3.get(i)+"), MoM("+Rev_BBand_inc.get(i)+")\n\n"
                                +"Revenue Digital :" +"Des 2017("+Rev_Digital_month1.get(i)+"), Jan 2018("+Rev_Digital_month2.get(i)+"), Feb 2018("+Rev_Digital_month3.get(i)+"), MoM("+Rev_Digital_inc.get(i)+")\n\n"
                                +"Revenue GSM :" +"Des 2017("+Rev_GSM_month1.get(i)+"), Jan 2018("+Rev_GSM_month2.get(i)+"), Feb 2018("+Rev_GSM_month3.get(i)+"), MoM("+Rev_GSM_inc.get(i)+")\n\n"
                                +"Revenue DCS :" +"Des 2017("+Rev_DCS_month1.get(i)+"), Jan 2018("+Rev_DCS_month2.get(i)+"), Feb 2018("+Rev_DCS_month3.get(i)+"), MoM("+Rev_DCS_inc.get(i)+")\n\n"
                                +"Revenue 3G :" +"Des 2017("+Rev_3G_month1.get(i)+"), Jan 2018("+Rev_3G_month2.get(i)+"), Feb 2018("+Rev_3G_month3.get(i)+"), MoM("+Rev_3G_inc.get(i)+")\n\n"
                                +"Revenue IR :" +"Des 2017("+Rev_IR_month1.get(i)+"), Jan 2018("+Rev_IR_month2.get(i)+"), Feb 2018("+Rev_IR_month3.get(i)+"), MoM("+Rev_IR_inc.get(i)+")\n\n"
                                +"Revenue ALL :" +"Des 2017("+Rev_ALL_month1.get(i)+"), Jan 2018("+Rev_ALL_month2.get(i)+"), Feb 2018("+Rev_ALL_month3.get(i)+"), MoM("+Rev_ALL_inc.get(i)+")\n"

                        )




                    .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.green)));

屏幕拍摄:

enter image description here

1 个答案:

答案 0 :(得分:0)

一个建议是先定义你的颜色

BitmapDescriptor green=
BitmapDescriptorFactory.fromResource(R.drawable.green); //etc etc


for(int i = 0 ; i < SITE_ID.size() ; i++){
    Double lat = Double.parseDouble(LAT.get(i));
    Double longi = Double.parseDouble(LONG.get(i));
    mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(lat,longi))
                    .title(SITE_ID.get(i))
                    .snippet(" <SNIP... YOUR SNIPPET>" )
                    .icon( ( //ternary operator
                        rev >200_000_000 ? red: 
                        rev >100_000_000 ? yellow:
                        rev > 60_000_000 ? white:
                        rev > 30_000_000 ? green:
                        black  
                    ) );

三元运算符将选择正确使用的bitmapDescriptor。

您也可以将三元运算符提取到自己的方法中。

private BitmapDescriptor getColor(long rev){
  return  rev >200_000_000 ? red: 
   rev >100_000_000 ? yellow:
   rev > 60_000_000 ? white:
   rev > 30_000_000 ? green:
   black ;
}

然后使用

调用该方法
    .icon (getColor(rev));