簡體   English   中英

以編程方式更改ICON googlemaps

[英]Change ICON programmatically googlemaps

我有一張地圖,其中顯示了一些存儲在數據庫(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;

}

嘗試這種方法。

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

這將忽略大寫和小寫的BLUE或blue它將同時運行

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

}

嘗試這個:

  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創建為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循環代碼是直接從我的項目中復制的,該項目實際上突出顯示了我在地圖活動中150個以上的標記中的微調框內選定的標記。

我想念的事,告訴我可以提供幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM