以编程方式更改ICON googlemaps

时间:2017-10-19 02:44:13

标签: android google-maps google-maps-markers programmatically

我有一张地图,其中我显示了一些存储在db(MySQL)中的标记,对于每个标记,还有一些其他字段(例如名称,地址,类别等)我想要的做比较如果字段“类别”等于“类别A”更改标记的图标,我怎样才能使这成为可能?任何想法都表示赞赏!

我正在尝试这样的事情,但它没有成功:

 if(location.get(i).get("campo_categoria").toString()=="Obras publicas")
        //if (name=="Obras publicas")
        {
            new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_op));

主:

ArrayList<HashMap<String, String>> location = null;
    String url = "http://appserver.puertovallarta.gob.mx/movil/getLanLong2.php";
    try {

        JSONArray data = new JSONArray(getHttpGet(url));
        location = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;

        for(int i = 0; i < data.length(); i++){
            JSONObject c = data.getJSONObject(i);
            map = new HashMap<String, String>();
            map.put("id", c.getString("id"));
            map.put("campo_latitud", c.getString("campo_latitud"));
            map.put("campo_longitud", c.getString("campo_longitud"));
            map.put("campo_categoria", c.getString("campo_categoria"));
            location.add(map);
        }
    } catch (JSONException e) {
// TODO Auto-generated catch block
        e.printStackTrace();
    }

    String campouno = "";
    if(!TextUtils.isEmpty(campouno)){
        double campo_latitud = Double.parseDouble(campouno);
    }
    //campo_latitud = Double.parseDouble(location.get(0).get("Latitude").toString());
    String campodos = "";
    if(!TextUtils.isEmpty(campodos)){
        double campo_longitud = Double.parseDouble(campodos);
    }

    for (int i = 0; i < location.size(); i++) {
        if(!TextUtils.isEmpty(location.get(i).get("campo_latitud").toString())&&!TextUtils.isEmpty(location.get(i).get("campo_longitud").toString())) {
            campo_latitud = Double.parseDouble(location.get(i).get("campo_latitud").toString());
            campo_longitud = Double.parseDouble(location.get(i).get("campo_longitud").toString());
        }

    String name = location.get(i).get("campo_categoria").toString();

        LatLng downtown = new LatLng(20.663203, -105.228053);
        googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(campo_latitud, campo_longitud))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
            .title(name));

        if(location.get(i).get("campo_categoria").toString()=="Obras publicas")
        //if (name=="Obras publicas")
        {
            new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_op));
        }

        googleMap.moveCamera(CameraUpdateFactory.newLatLng(downtown));
    googleMap.setLatLngBoundsForCameraTarget(ADELAIDE);
}}

PHP文件:

<?php
require_once 'dbDetails.php';
$sql = "SELECT * FROM `reportes2` ORDER BY id ASC";
$objQuery = mysqli_query($con,$sql);

$arrRows = array();
$arryItem = array();

while($arr = mysqli_fetch_array($objQuery)) {

$arryItem["id"] = $arr["id"];
$arryItem["campo_latitud"] = $arr["campo_latitud"];
$arryItem["campo_longitud"] = $arr["campo_longitud"];
$arryItem["campo_categoria"] = $arr["campo_categoria"];
$arryItem["campo_descripcion"] = $arr["campo_descripcion"];

$arrRows[] = $arryItem;

}

2 个答案:

答案 0 :(得分:4)

尝试这种方法。!

String BLUE_COLOR="blue";
String RED_COLOR="red";

      String campoCategoria= location.get(i).get("campo_categoria").toString(); 
            googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(campo_latitud, campo_longitud))
                .icon(BitmapDescriptorFactory.fromResource(getIconUsingSwitch(campoCategoria)))
                .title(name));


 private int void getIconUsingSwitch(String campoCategoria) {
            switch (campoCategoria) {
            case "blue":
                return R.drawable.ic_blue;
                break;
            case "red":
                return R.drawable.ic_red;
                break;
            default:
                return R.drawable.ic_normal;
            }
        }

表示使用字符串equalsIgnoreCase,使用对象equals

  

这将忽略大写和小写蓝色或蓝色它将为两个

运行
  void getIconUsingIf(String campoCategoria) {

    if (campoCategoria.equalsIgnoreCase(BLUE_COLOR)) {
        return R.drawable.ic_blue;
    } else if (campoCategoria.equalsIgnoreCase(RED_COLOR)) {
        return R.drawable.ic_red;
    } else {
        return R.drawable.ic_normal;
    }

}

答案 1 :(得分:0)

试试这个:

  LatLng downtown = new LatLng(20.663203, -105.228053);
  for (int i = 0; i < location.size(); i++) {
            if(!TextUtils.isEmpty(location.get(i).get("campo_latitud").toString())&&!TextUtils.isEmpty(location.get(i).get("campo_longitud").toString())) {
                campo_latitud = Double.parseDouble(location.get(i).get("campo_latitud").toString());
                campo_longitud = Double.parseDouble(location.get(i).get("campo_longitud").toString());
            }
            String name = location.get(i).get("campo_categoria").toString();    
            if (Objects.equals(location.get(i).get("campo_categoria").toString(),"Obras publicas")) {
                googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(campo_latitud, campo_longitud))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_op1))
            .title(name));

            } else if (Objects.equals(location.get(i).get("campo_categoria").toString(),"Any other Choice")) {
                googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(campo_latitud, campo_longitud))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_op2))
            .title(name));
            } 
            else {
                 googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(campo_latitud, campo_longitud))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
            .title(name));
            }
        }
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(downtown));
        googleMap.setLatLngBoundsForCameraTarget(ADELAIDE);

通过这种方式,您可以针对不同的类别使用不同的标记,并且可以使用其他许多标记 - 如果没有任何问题。我在我的17岁并且工作得很好。

另外,要管理所有这些标记,您可以将列表创建为List<Marker> list = new ArrayList<>();,然后可以在其中添加标记

Marker marker = googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(campo_latitud, campo_longitud))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_op1))
            .title(name));
list.add(marker);

然后,您可以从中访问任何标记:

   Marker m = list.get(5); //position of needed marker
   m.getTitle or m.getPosition or m.getAlpha //all these will then work

您也可以使用for循环:

for(Marker m:list){
if (m.getTitle().equals("Your Title")) {
            m.showInfoWindow();
            CameraPosition cameraPosition = new CameraPosition.Builder().target(m.getPosition()).zoom(14).build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            break;
        }

上面的循环代码是直接从我的项目中复制的,它实际上突出了150多个标记之间地图活动中微调器的选定标记。

我错过了什么,告诉我,我可以提供帮助。