Android Google Maps - SetOnInfoWindowClickListener

0

This is my situation, I have markers that show information from a JSON, what I want is to select a marker and that I sent an activity with the information associated with it, the problem is that, this step is not met, reviewing logcat, I noticed that when selecting a bookmark, it immediately sent the data of all the bookmarks, including the one I clicked on, attached code to complement the question.'public void onMapReady (GoogleMap googleMap) {

    ArrayList<WeakHashMap<String, String>> location = null;
    String url = "http://www.app.transportessalgado.cl/obtenercoordenadas.php";
    try {

        JSONArray data = new JSONArray(getHttpGet(url));

        location = new ArrayList<WeakHashMap<String, String>>();
        WeakHashMap<String, String> map;

        for (int i = 0; i < data.length(); i++) {
            JSONObject tienda = data.getJSONObject(i);

            map = new WeakHashMap<String, String>();
            map.put("id_estacionamiento", tienda.getString("id_estacionamiento"));
            map.put("usuario_rut_administrador", tienda.getString("usuario_rut_administrador"));
            map.put("nombre_estacionamiento", tienda.getString("nombre_estacionamiento"));
            map.put("latitud", tienda.getString("latitud"));
            map.put("longitud", tienda.getString("longitud"));
            map.put("cantidad_cupos", tienda.getString("cantidad_cupos"));
            Log.e("lo que recibe el mapa", String.valueOf(map));
            location.add(map);

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    for (int i = 0; i < location.size(); i++) {
        idestacionamiento = String.valueOf(location.get(i).get("id_estacionamiento"));
        name = String.valueOf(location.get(i).get("nombre_estacionamiento"));
        double latitude = Double.parseDouble(location.get(i).get("latitud"));
        double longitude = Double.parseDouble(location.get(i).get("longitud"));
        name2 = String.valueOf(location.get(i).get("cantidad_cupos"));
        name4 = String.valueOf(location.get(i).get("usuario_rut_administrador"));
        Log.e("lo que recibe el mapa", String.valueOf(location));


        name1 = "Nombre Estacionamiento: " + name;
        name3 = "Cupos: " + name2;
        String name5 = "Horario Atencion: 09:00 a 18:00";

        Marker marker = googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(latitude, longitude))
                .title(name1)
                .snippet(Arrays.toString(name3.split("[\r\n]+")) + name5));

        idmarcador = marker.getId();
        mHashMap.put(i, marker);


    }Log.e("lo que recibe el mapa", String.valueOf(location));

        final ArrayList<WeakHashMap<String, String>> finalLocation = location;
        googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {

                    LatLng lat_long = marker.getPosition();
                    Intent iratienda = new Intent(getApplicationContext(), ConfirmarReserva.class);
                    iratienda.putExtra("id_estacionamiento", idestacionamiento);
                    iratienda.putExtra("usuario_rut_administrador", name4);
                    iratienda.putExtra("nombre_estacionamiento", name);
                    iratienda.putExtra("cantidad_cupos", name2);
                    Log.e("Informacion_enviada", String.valueOf(finalLocation));
                    startActivity(iratienda);



            }
        });

    }
  

06-26 03: 51: 14.250 21745-21745 / com.fernandobrito.parking E / Submitted_information: [{length = -71.822754, latitude = -36.140648, quantity_cupos = 10, nombre_estacionamiento = johnito, user_rut_administrador = 1, id_estacionamiento = 1}, {length = -0.324234, latitude = -0.324324, quantity_quotas = 1, station_name = fernando, user_rut_manager = 1111, id_stage = 2}, {length = -71.824051, latitude = -36.142857, quantity_quotas = 100, name_seat = princess, administrator_rut_user = 1111, station_id = 4}, {length = -71.824043, latitude = -36.143135, number_quotas = 21, station_name = fffff, administrator_rut_user = 1111, station_id = 5}, {length = -71.817596, latitude = -36.144310, quantity_quotas = 22, staging_name = ttttt, administrator_rut_user = 1111, staging_id = 6}]

and here is the logcat, where when selecting a marker, instead of sending the information of the marker that was pressed, all those that are in the database are sent.

Thanks, I hope you have understood, and can help me :( '

    
asked by Fernando Brito 26.06.2018 в 09:56
source

1 answer

0

I solved my problem by changing the structure of the code a bit, I will leave the published code to help someone who has a similar problem.

 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 tienda = data.getJSONObject(i);

            Marker marker = googleMap.addMarker(new MarkerOptions()
                    .position(new LatLng(data.getJSONObject(i).getDouble("latitud"),
                            data.getJSONObject(i).getDouble("longitud")))
                    .title(data.getJSONObject(i).getString("nombre_estacionamiento")).flat(true)
                    .snippet(data.getJSONObject(i).getString("id_estacionamiento"))
                    .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.automovil))));

                    final String rutadministrador= data.getJSONObject(i).getString("usuario_rut_administrador");

                marker.showInfoWindow();
                idmarcador=marker.getId();
                mHashMap.put(i,marker);

                final ArrayList<HashMap<String, String>> Location = location;

            googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                    @Override
                    public void onInfoWindowClick(Marker marker) {

                        String idmarker= marker.getId();
                       String titulo=marker.getTitle();
                       String idestacionamiento=marker.getSnippet();


                       //float rutadministrador=marker.getAlpha();
                       //String rutadmi=String.valueOf(rutadministrador);

                        Intent i = new Intent(getApplicationContext(), ConfirmarReserva.class);

                        i.putExtra("id_estacionamiento",idestacionamiento );
                        i.putExtra("usuario_rut_administrador", rutadministrador);
                        i.putExtra("nombre_estacionamiento", titulo);
                        Log.e("Informacion_enviada", valueOf(Location));
                        startActivity(i);


                        }
                        }
                );

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }


}
    
answered by 28.06.2018 в 05:40