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