Marker id pass to setOnInfoWindowClickListener

Asked

Viewed 157 times

1

I’m trying to pass the value of the Marketer to the setOnInfoWindowClickListener, but I’m not sure what is the best way to do this, can anyone help me? It even passes a value, but not the loop, IE, is not passing the value of Marker by id. my code.

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

2 answers

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 a class instance

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

hope I’ve 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());

        }

    }
}
  • Thanks for the answer, I’m using this loop from the Marker inside the onPostExecute, I create this class Myoninfowindowclicklistener out of the loop already with the maker and then call with a new Class() ?

  • You can create within the same class, or another class. You can pass Mapascontroller mc, as parameter too.

  • Hello, I’m having a hard time putting the information you gave me in my code, can you help me a little more?

  • Oops, sorry to keep you waiting. where are you trying to put the code??

  • Hello, no problem, I put the full code in the question, I’m having difficulty creating this class that suggested, in my code, I actually wanted to apply the code keeping the architecture that already has in the code

  • I edited the answer, instead of implementing the Listener in the method call itself, just create a class implementing the Listener, and in the method call, you create a class instance, passing as many parameters as necessary.

  • Gee, I made the changes and keeps showing only 1 item.. rs I don’t know where I’m going wrong.. rs

  • What do you mean just 1 item? I don’t understand you

  • So, I need each marketer to pass their own id to the next Activity, but it only passes 1 id, and it’s the first.. I can click on how many markers it is, which only goes in the same always. Even with these changes you told me, it keeps going the same

Show 5 more comments

1


In case someone has the same problem, I was able to solve it in the following way:

I put the hasmap instead

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

Then in the code I passed the id of the Marker:

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

And finally, I got a get on mmap.setOnInfoWindowClickListener and compared the id of the Marker with the id of the post.

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

Upshot:

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

                }



            }

        });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.