【问题标题】:Change ICON programmatically googlemaps以编程方式更改 ICON googlemaps
【发布时间】:2017-10-19 02:44:12
【问题描述】:

我有一张地图,其中显示了一些存储在数据库(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;

}

【问题讨论】:

  • 你可以添加 $sql = "SELECT * FROM reportes2 WHERE YourNo=1 ORDER BY id ASC";`.
  • 感谢@LalitSinghFauzdar,工作顺利,你帮了我很多忙!
  • 没问题,伙计。
  • 兄弟@LalitSinghFauzdar,我有一个问题我想说“else if(location.get(i).get("campo_categoria").toString().equals("Obras publicas") &&(location.get(i).get("campo_estado").toString().equals("En proceso"))){ googleMap.addMarker(new MarkerOptions().position(new LatLng(campo_latitud, campo_longitud))。标题(名称).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_pa)));"但没有看到标记,这是正确的吗?
  • 顺便说一句,我通过写这个来解决条件问题:“if(location.get(i).get("campo_estado").toString().equals("En proceso")&&location. get(i).get("campo_categoria").toString().equals("Ecolog?a")){googleMap.addMarker...(R.drawable.ic_eco_yellow)));}elseif(location.get(i ).get("campo_estado").toString().equals("Pendiente")&&location.get(i).get("campo_categoria").toString().equals("Ecolog?a")){googleMap.addMarker ...(R.drawable.ic_eco_red)));}"

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


【解决方案1】:

试试这个方法!

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

}

【讨论】:

  • 使用if有什么缺点吗?我也可以试试你贴的这个方法
  • 欢迎@Armando 尝试让你的编程技能变得干净。使用最好的方法(专业方法)而不是使用不专业的方法。如果你继续使用不正确的编程方式,你可能会成为程序员,但不是一个好人高效的程序员!
  • @Armando 您在方法中使用 For 循环,然后添加 if 会使它更慢。并且永远不要使用 compare if == this 我永远不会推荐这个东西。!其不专业的方式。!
  • 这就是我说使用 Objects.equals 的原因。对字符串使用 == 是不合适的,请参阅 here
  • @AtifAbbAsi 您发布此答案的那一刻,这是一种非常干净的方式,但现在不是。为什么?因为在这个方法里面使用方法调用+if显然比只用if-else要慢。除非你打了一百万个电话,否则这无关紧要。另外,要指出一点,他为什么要费心使用函数调用+ if。而且我认为您没有阅读,我在 for 循环中使用了 17 个 if-elseif-else,它实际上放置了我所有的标记(165)和折线(所有这些标记之间的 8 条折线),我无法发现任何设备中的一秒延迟。
【解决方案2】:

试试这个:

  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&lt;Marker&gt; list = new ArrayList&lt;&gt;();,然后可以在其中添加标记为

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 循环代码是直接从我的项目中复制而来的,它实际上在 150 多个标记之间从我的地图活动中的微调器中突出显示了选定的标记。

我错过了什么,告诉我我可以帮忙。

【讨论】:

  • 谢谢,我刚刚添加了 "String name = location.get(i).get("campo_categoria").toString(); if (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(姓名));”部分,但是在运行应用程序时我看不到任何标记,我不知道为什么
  • @Armando 你为什么要仔细检查它?只需使用if (Objects.equals(name,"Obras publicas")
  • 除此之外,Objects.euqals 应该与字符串一起使用,因为 == 检查项目类型。
  • 非常感谢,它现在可以工作了,我只是将 == 更改为 .equals,我还有一个问题,我可以发布它让您知道吗?
  • 好吧,我现在要做的是在数据库中存储了一个数字(1 和 0)在一个字段中(例如接受的“1”),所以如果数字是 0,那么不要t show this marker if it is 1 show it,我不知道我是否清楚?
猜你喜欢
  • 1970-01-01
  • 2019-04-10
  • 2015-08-16
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多