以编程方式更改图标谷歌地图

Change ICON programmatically googlemaps

我有一张地图,其中显示了一些存储在数据库 (MySQL) 中的标记,对于每个标记,还有一些其他字段(例如名称、地址、类别等) .) 我想做的是比较字段 "category" 是否等于 "category 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;

}

试试这个:

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

这样您就可以针对不同的类别使用不同的标记,并且您可以毫无问题地使用许多 else-if。我有 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;
        }

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

我遗漏了什么,告诉我我可以帮忙。

试试这个方法!

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

this will ignore Upper case and lower case BLUE or blue it will run for both

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

}