Marker pass id to setOnInfoWindowClickListener

1

I'm trying to pass the value from the marker to the setOnInfoWindowClickListener, but I'm not sure what the best way for this is, can anyone help me? It even passes a value, but not the loop, that is, it is not passing the value of the marker by id. my code.

Full Code

    private class MapaGet extends AsyncTask<String, Void, Void>  implements GoogleMap.OnInfoWindowClickListener {

    // Lista onde vamos guardar os valores
    List<MapasController> ListaDeController = new ArrayList<MapasController>(0);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(ViewMaps.this);
        pDialog.setMessage("Por favor, aguarde...");
        pDialog.setCancelable(true);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(String... arg0) {
        // Creating service handler class instance
        cx =  new Conexao();
        String jsonStr = cx.get(url);

        Log.d("Resposta: ", "> " + jsonStr);

        if (jsonStr != null) {

            try {
                // De-serialize the JSON
                JSONArray jsonArray = new JSONArray(jsonStr);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObj = jsonArray.getJSONObject(i);

                    MapasController mc = new MapasController();


                    mc.setLatitude(jsonObj.getDouble(TAG_LATITUDE));
                    mc.setLongitude(jsonObj.getDouble(TAG_LONGITUDE));

                    mc.setTitulo(jsonObj.getString(TAG_TITULO));
                    mc.setDescricao(jsonObj.getString(TAG_DESCRICAO));
                    mc.setId(jsonObj.getString(TAG_ID));
                    mc.setStatus(jsonObj.getString(TAG_STATUS));
                    mc.setData(jsonObj.getString(TAG_DATA));
                    mc.setUsuario(jsonObj.getString(TAG_USUARIO));
                    mc.setCategoria(jsonObj.getString(TAG_CATEGORIA));

                    // Adicionamos na lista
                    ListaDeController.add(mc);



                    Log.d ("", "get " + mc.getCategoria() + "Longitude " + mc.getLatitude() + " "  + jsonObj.getString(TAG_TITULO) );


                }
            } catch (JSONException e) {
                Log.e("tag", "Error processing JSON", e);
            }


        } else {
            Log.e("Get: ", "Não foi possível obter quaisquer dados do url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        // Se o tamanho da lista fo maior que zero!
        if(ListaDeController.size() > 0){

            for(final MapasController mc  : ListaDeController){

                LatLng local = new LatLng(mc.getLatitude(), mc.getLongitude());

                // Seleciona o ícone de acordo com a categoria do mesmo

                Marker mapa =  mMap.addMarker(new MarkerOptions()
                        .position(local)
                        .title(mc.getTitulo())
                        .snippet(mc.getDescricao())
                        .draggable(true)
                        .icon(BitmapDescriptorFactory.fromResource(icone))

                );
                mMap.getCameraPosition();
                mMap.moveCamera(CameraUpdateFactory.newLatLng(local));
                mMap.getUiSettings().setZoomControlsEnabled(true);

                mMap.animateCamera(CameraUpdateFactory.zoomTo(19), 2000, null); // animação zoom 10 com 2 segundos


            }



            mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

                @Override
                public void onInfoWindowClick(Marker marker) {

                    Intent in = new Intent(ViewMaps.this, DescricaoMaps.class);

                    in.putExtra(TAG_ID, mc.getId());
                    in.putExtra(TAG_TITULO, mc.getTitulo());
                    in.putExtra(TAG_DESCRICAO, mc.getDescricao());
                    in.putExtra(TAG_DATA, mc.getData());
                    in.putExtra(TAG_STATUS, mc.getStatus());
                    in.putExtra(TAG_USUARIO, mc.getUsuario());
                    in.putExtra(TAG_CATEGORIA, mc.getCategoria());

                    ViewMaps.this.startActivity(in);


                }
            });

        }



        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

}




@Override
public void onMapReady(GoogleMap googleMap) {

    mMap = googleMap;


    // se o mapa não está nulo , vamos carregar
    if (mMap != null) {
        new MapaGet().execute();
    }
}
    
asked by anonymous 10.08.2016 / 14:28

2 answers

1

If someone has the same problem, I was able to resolve it as follows:

I've placed the hasmap before

 HashMap<String, String> markerMap = new HashMap<String, String>();

Then in the code I passed the marker id:

 String id = marker.getId();
            markerMap.put(id, mc.getId());

And finally I did a get in mMap.setOnInfoWindowClickListener and compared the marker id with the post id.

if (mc.getId().equals(markerMap.get(marker.getId()))){

Result:

 @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        // Se o tamanho da lista fo maior que zero!
        if(ListaDeController.size() > 0){

         //  for (int i = 0; i < ListaDeController.size(); i++){

            for(final MapasController mc  : ListaDeController){

              //  MapasController mc = new MapasController();
                LatLng local = new LatLng(mc.getLatitude(), mc.getLongitude());

                // Seleciona o ícone de acordo com a categoria do mesmo

                  Marker marker = mMap.addMarker(new MarkerOptions()
                        .position(local)
                        .title(mc.getTitulo())
                        .snippet(mc.getDescricao())
                        .draggable(true)

                );

                mMap.getCameraPosition();
                mMap.moveCamera(CameraUpdateFactory.newLatLng(local));
                mMap.getUiSettings().setZoomControlsEnabled(true);

                mMap.animateCamera(CameraUpdateFactory.zoomTo(19), 2000, null); // animação zoom 10 com 2 segundos

              //  int id = 0;
                //int id = Integer.parseInt(mc.getId());

                String id = marker.getId();
                markerMap.put(id, mc.getId());


            }

        }

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


                Intent in = new Intent(ViewMaps.this, DescricaoMaps.class);


                for(final MapasController mc  : ListaDeController){

                    if (mc.getId().equals(markerMap.get(marker.getId()))){

                    in.putExtra(TAG_ID, mc.getId());
                    in.putExtra(TAG_TITULO, mc.getTitulo());
                    in.putExtra(TAG_DESCRICAO, mc.getDescricao());
                    //  in.putExtra(TAG_DATA, mc.getData());
                    //  in.putExtra(TAG_STATUS, mc.getStatus());
                    //  in.putExtra(TAG_USUARIO, mc.getUsuario());
                    //  in.putExtra(TAG_CATEGORIA, mc.getCategoria());
                    Log.d("Passando parametro:", "Teste" + mc.getTitulo() + mc.getDescricao());
                    startActivity(in);
                    }

                }



            }

        });
    
16.08.2016 / 15:32
2

Create a class that extends GoogleMap.OnInfoWindowClickListener

for example

public class MyOnInfoWindowClickListener extends GoogleMap.OnInfoWindowClickListener {

    private int mParametro = 0;

    public MyOnInfoWindowClickListener(int parametro) {
        this.mParametro = parametro;
    }

    @Override
    public void onInfoWindowClick(Marker marker) {
       //implementar o método, pode usar o mParametro aqui dentro

    }
}

and in the setOnInfoWindowClickListener, just create an instance of the class

int id = 3;
mMap.setOnInfoWindowClickListener(new MyOnInfoWindowClickListener(id))

I hope to have helped!

[EDIT]

Try this

private class MapaGet ... {


  //...
  //seu codigo
  //

   @Override
    protected void onPostExecute(Void result) {
      //...
      //...
      int id = 0;
      mMap.setOnInfoWindowClickListener(new MyOnInfoWindowClickListener(id, mc));
    }

    public class MyOnInfoWindowClickListener extends GoogleMap.OnInfoWindowClickListener {

        private int mParametro = 0;
        private MapasController mMapasController = null;

        public MyOnInfoWindowClickListener(int parametro, MapasController mapasController) {
            this.mParametro = parametro;
            this.mMapasController = mapasController;
        }

        @Override
        public void onInfoWindowClick(Marker marker) {
           //implementar o método, pode usar o mParametro aqui dentro

           MapasController mc = mMapasController;
           Intent in = new Intent(ViewMaps.this, DescricaoMaps.class);

              in.putExtra(TAG_ID, mc.getId());
              in.putExtra(TAG_TITULO, mc.getTitulo());
              in.putExtra(TAG_DESCRICAO, mc.getDescricao());
              in.putExtra(TAG_DATA, mc.getData());
              in.putExtra(TAG_STATUS, mc.getStatus());
              in.putExtra(TAG_USUARIO, mc.getUsuario());
              in.putExtra(TAG_CATEGORIA, mc.getCategoria());

        }

    }
}
    
10.08.2016 / 15:17