【问题标题】:Use Custom INFOWINDOW with MySQL将自定义 INFOWINDOW 与 MySQL 一起使用
【发布时间】:2018-04-01 03:57:23
【问题描述】:

我有一张地图,我在其中显示存储在数据库(MySQL)中的标记,每个标记都有不同的字段(例如:姓名、电子邮件、地址等),因为“MarkerOptions”只让我使用“ .tittle",".sn-p"我想附加的一些字段被遗漏了,我现在知道我必须使用自定义信息窗口来显示我想要的所有字段。我怎样才能做到这一点? 我的代码:

主要:

ArrayList<HashMap<String, String>> location = null;
    String url = "http://appserver.eco.mx/movil/getLanLong.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"));
            map.put("campo_estado", c.getString("campo_estado"));
            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();
        //String des = location.get(i).get("campo_descripcion").toString();

        if(location.get(i).get("campo_categoria").toString().equals("Obras publicas")){
            //System.out.println(location.get(i).get("campo_descripcion").toString());
            googleMap.addMarker(new MarkerOptions().position(new LatLng(campo_latitud, campo_longitud)).title(name).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_op)));
        }
  }}

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/white">

<TextView
    android:id="@+id/info_window_nombre"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:paddingLeft="5dp"
    android:text="Carlo Estrada Solano" />

<TextView
    android:id="@+id/info_window_placas"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textSize="20sp"
    android:layout_below="@id/info_window_nombre"
    android:paddingLeft="5dp"
    android:text="Placas: SX5487" />

<TextView
    android:id="@+id/info_window_estado"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/info_window_placas"
    android:paddingLeft="5dp"
    android:text="Estado: Activo" />

</LinearLayout>

信息窗口类:

public class AdaptadorInforWindow implements GoogleMap.InfoWindowAdapter {

private static final String TAG = "CustomInfoWindowAdapter";
private LayoutInflater inflater;

public AdaptadorInforWindow(LayoutInflater inflater){
    this.inflater = inflater;
}

@Override
public View getInfoWindow(Marker marker) {
    //Carga layout personalizado.
    View v = inflater.inflate(R.layout.infowindow_layout, null);
    String[] info = marker.getTitle().split("&");
    String url = marker.getSnippet();
    ((TextView)v.findViewById(R.id.info_window_nombre)).setText("Lina Cortés");
    ((TextView)v.findViewById(R.id.info_window_placas)).setText("Placas: SRX32");
    ((TextView)v.findViewById(R.id.info_window_estado)).setText("Estado: Activo");

    return v;
}

@Override
public View getInfoContents(Marker marker) {
    return null;
    }
}

设置谷歌地图:

myGoogleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(LayoutInflater.from(getActivity())));

【问题讨论】:

  • 代码看起来不错。有什么问题吗?
  • 我的意思是它工作得很好,但我没有做我需要的,我想使用服装信息窗口,但在 xml 中我写了姓名、地址等,我想使用名称,来自数据库的地址并将其设置为服装信息窗口@LalitSinghFauzdar

标签: android mysql google-maps-markers infowindow


【解决方案1】:

使用标记键将标记存储在 Hashmap 中。然后使用使用 hashmap 对象的自定义信息窗口来检索其他信息。 当您单击标记时,它将显示相关信息。

或者您可以从病房的 8:00 开始观看此视频。 Click Here

【讨论】:

  • 如何使用标记键?我看到了视频,但我没有看到从数据库中获取值的位置
  • 我能够做到我提到的,但你的信息对我帮助很大,谢谢!
  • @Armando 如果您觉得答案有帮助,请接受它作为解决方案。
【解决方案2】:

所以我刚刚为我的应用创建了一个CustomInfoWindow。以下是实现这一目标的方法。

首先在您的数据库文件中创建一个方法,用于访问与您的标记相关的所有数据,代码如下:

   Cursor getDetails(Marker marker){            
            db = getReadableDatabase();        
            String query = "select Name, Email, Address from YourTableName where Name = '"+marker.getTitle()+"';";
            return db.rawQuery(query,null);
   }

然后如下调整 AdaptadorInforWindow 的代码:

    YourDatabaseClass db = new YourDatabaseClass(context);
    Cursor c = db.getDetails(); //Method you'll create in database class
    ((TextView)v.findViewById(R.id.info_window_name)).setText(c.getString(0));
    ((TextView)v.findViewById(R.id.info_window_email)).setText(c.getString(1));
    ((TextView)v.findViewById(R.id.info_window_address)).setText(c.getString(2)); 
    c.close();

看,getString 被使用是因为我猜你已经使用文本/字符串数据类型来存储你的姓名、电子邮件和地址值。请记住,如果数据类型发生变化,那么您必须使用适当的方法,如 c.getInt(index) 用于 int 等。

此外,索引 0,1,2 与查询中的相同:名称电子邮件地址。

这样您的数据就会显示在您的CustomInfoWindow 中。

Same 在我的工作中表现出色,我很确定这会奏效。

【讨论】:

  • 你的意思是添加这个“光标 getDetails(Marker marker){ db = getReadableDatabase(); String query = "select Name, Email, Address from YourTableName where Name = '"+marker.getTitle()+ "';"; 在 php 文件中返回 db.rawQuery(query,null); }" 兄弟?
  • 啊!!!我不知道您使用的是 PHP ... @Armando 但您可以自定义它以获取当前标记的详细信息,不是吗?
  • 而且我非常有信心它在我的工作中有效,肯定会为您工作......
  • 是的,它的 PHP,并不是我的意思是我唯一可以设置的是使用 .tittle 和 .sn-p 的两个属性
猜你喜欢
  • 2017-09-30
  • 2017-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多