Use Custom INFOWINDOW with MySQL

2019-08-30 00:41发布

问题:

I have a map where i display markers that are stored in a db (MySQL),each marker have different fields with it (for example: name, email, addres, etc), since "MarkerOptions" only let me use ".tittle",".snippet"some of the fields that i want to attach are left out, i know now that i have to use a custom infowindow to show all the fields that i want to. How can i do this? My code:

Main:

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>

InfoWindow class:

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

set googlemap:

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

回答1:

Store markers in Hashmap by Using the marker key. Then use the Custom infowindow using hashmap object to retrieve the others information. When you will click marker it will show the related information.

or you can watch this video from 8:00 on wards. Click Here



回答2:

So I've just made a CustomInfoWindow for my app. Here's how you can achieve this.

First Create a method in your database file just to access all the data related to your marker and the code is as follows:

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

Then tune your AdaptadorInforWindow's code as follows:

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

See, getString is used because I guess you've used Text/String datatype to store your Name, Email and Address Values. Remember if datatype changes, then you have to use the appropriate method as c.getInt(index) for int and so on.

Also, Indices 0,1,2 are same as in query: Name Email Address.

This way you'll have your data shown in your CustomInfoWindow.

Same is working in mine superbly and I'm pretty sure this will work.