Search code examples
androidgoogle-mapsgoogle-maps-api-3google-maps-markersmarkerclusterer

Android setOnMarkerClickListener set title for each marker


I've implemented a clustermarker in my app, I have a list of LatLng that comes from my database, now I want to show the name of the clients as title when the user clicks on the marker,but when I click on the marker it shows me nothing, how can I achieve that, this is my code so far:

    public class MapaViagem extends FragmentActivity {

    private GoogleMap googleMap;
    private String rm_IdViagem;
    private List<ClienteModel> mClienteModel = new ArrayList<ClienteModel>();
    private List<EnderecoModel> mEnderecoModel = new ArrayList<EnderecoModel>();
    private ViagemModel mViagemModel = new ViagemModel();
    private ClusterManager<MyItem> mClusterManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.maps);
         ArrayList<LatLng> coordList = new ArrayList<LatLng>();

        try {

            Bundle parametros = getIntent().getExtras();
            rm_IdViagem = parametros.getString("id_viagem");

            Repositorio ca = new Repositorio(this);
            mViagemModel = ca.getViagemPorId(Integer.valueOf(rm_IdViagem));

            Repositorio cl = new Repositorio(this);
            mClienteModel = cl.getClientesViagem(Integer.valueOf(rm_IdViagem));

            int i;

            for ( i = 0; i < mClienteModel.size(); i++) {


                Repositorio mRepositorio = new Repositorio(this);
                mEnderecoModel = mRepositorio.getListaEnderecosDoCliente(Integer.valueOf(mClienteModel.get(i).getClientes_id()));


                System.out.println("NOMES " + mClienteModel.get(i).getNome());


                for (int j = 0; j < mEnderecoModel.size(); j++) {
                    // Loading map
                    initilizeMap();
                    // Changing map type
                    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                    // googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                    // googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                    // googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                    // googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);

                    // Showing / hiding your current location
                    googleMap.setMyLocationEnabled(true);

                    // Enable / Disable zooming controls
                    googleMap.getUiSettings().setZoomControlsEnabled(true);

                    // Enable / Disable my location button
                    googleMap.getUiSettings().setMyLocationButtonEnabled(true);

                    // Enable / Disable Compass icon
                    googleMap.getUiSettings().setCompassEnabled(true);

                    // Enable / Disable Rotate gesture
                    googleMap.getUiSettings().setRotateGesturesEnabled(true);

                    // Enable / Disable zooming functionality
                    googleMap.getUiSettings().setZoomGesturesEnabled(true);


                    final float latitude = Float.parseFloat(mEnderecoModel.get(j).getLatitude());
                    final float longitude = Float.parseFloat(mEnderecoModel.get(j).getLongitude());


                    coordList.add(new LatLng(latitude, longitude));


                    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 10));



                    // Initialize the manager with the context and the map.
                    // (Activity extends context, so we can pass 'this' in the constructor.)
                    mClusterManager = new ClusterManager<MyItem>(this, googleMap);

                    // Point the map's listeners at the listeners implemented by the cluster
                    // manager.
                    googleMap.setOnCameraChangeListener(mClusterManager);
                    googleMap.setOnMarkerClickListener(mClusterManager);








                    addItems(coordList);


                    googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

                        @Override
                        public boolean onMarkerClick(final Marker marker) {
                            LatLng pos = marker.getPosition();
                            int arryListPosition = getArrayListPosition(pos);


                            return true;
                        }
                    });



                }


            }


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

    private int getArrayListPosition(LatLng pos) {

        for (int i = 0; i < mEnderecoModel.size(); i++) {
            if (pos.latitude == Double.parseDouble(mEnderecoModel.get(i).getLatitude().split(",")[0])) {
                if (pos.longitude == Double.parseDouble(mEnderecoModel.get(i).getLongitude().split(",")[1]))
                    return i;
            }
        }
        return 0;
    }

    private void addItems(List<LatLng> markers) {

        for (int i = 0; i < markers.size(); i++) {
            MyItem offsetItem = new MyItem(markers.get(i));
            mClusterManager.addItem(offsetItem);
        }
    }



    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Não foi possível carregar o mapa", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }


}

Solution

  • Not sure how you are initializing your Markers because you don't show that part of the code but put a title on it is done this way:

    //You need a reference to a GoogleMap object
    GoogleMap map = ... // get a map.
    Marker marker = map.addMarker(new MarkerOptions()
     .position(new LatLng(37.7750, 122.4183))     //Or whatever coordinate
     .title("Your title")
     .snippet("Extra info"));
    

    And that's it, when you click it you'll see the info of that marker.

    You can find more information here

    EDIT

    For the ClusterManagerI think you can find this answer helpful