Search code examples
androidgoogle-mapsgoogle-maps-api-3markerclusterer

Custom information in InfoWindow after pressing a marker using Map Cluster from Google Map utils


I am trying to render a custom InfoWindow with different values for each marker using MarkerCluster

This is what i try:

private final GoogleMap.InfoWindowAdapter mInfoWindowAdapter = new GoogleMap.InfoWindowAdapter() {
        @Override
        public View getInfoWindow(Marker marker) {
            View window = null;
            if(getActivity()!=null&&isAdded()){
                window = getActivity().getLayoutInflater().inflate(R.layout.map_objective_overlay, null);
                final CustomFontTextView nameTV = (CustomFontTextView) window.findViewById(R.id.nameTV);

                if(clickedClusterItem!=null){
                    System.out.println("You clicked this: "+clickedClusterItem.getName());
                }else{
                    System.out.println("The clicked cluster item was nulllll");
                }
                if (clickedCluster != null) {
                    for (Objective item : clickedCluster.getItems()) {
                        // Extract data from each item in the cluster as needed
                        if(item.getRemoteId().equals(clickedClusterItem.getRemoteId())){
                            nameTV.setText(clickedClusterItem.getName());
                        }
                    }
                }
            }
            return window;
        }

        @Override
        public View getInfoContents(Marker marker) {
            return null;
        }
    };
    private Objective clickedClusterItem;
    private Cluster<Objective> clickedCluster;

@Override
    public void onResume() {
        super.onResume();
        preferences = getActivity().getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE);
        if (map == null) {
            map = fragment.getMap();
            mClusterManager = new ClusterManager<Objective>(getActivity(), map);
            map.setOnCameraChangeListener(mClusterManager);
            map.setOnMarkerClickListener(mClusterManager);
            latitude = Double.parseDouble(preferences.getString(Constants.LATITUDE, "0"));
            longitude = Double.parseDouble(preferences.getString(Constants.LONGITUDE, "0"));
            map.addMarker(new MarkerOptions()
                    .position(new LatLng(latitude, longitude))
                    .title("Hello " + preferences.getString(Constants.TOURIST_NAME, "tourist"))
                    .snippet("You are here")
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_check_in)));
            map.setInfoWindowAdapter(mClusterManager.getMarkerManager());
            //mClusterManager.setRenderer(new ObjectiveClusterRenderer(getActivity(), map, mClusterManager));
            mClusterManager.getClusterMarkerCollection().setOnInfoWindowAdapter(mInfoWindowAdapter);
            mClusterManager.getMarkerCollection().setOnInfoWindowAdapter(mInfoWindowAdapter);
            map.setOnMarkerClickListener(mClusterManager);

            mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<Objective>() {
                @Override
                public boolean onClusterClick(Cluster<Objective> cluster) {
                    clickedCluster = cluster; // remember for use later in the Adapter
                    return false;
                }
            });
            mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<Objective>() {
                @Override
                public boolean onClusterItemClick(Objective item) {
                    clickedClusterItem = item;
                    return false;
                }
            });

And here i add the markers to the clustermanager:

public void setUpClusterer(List<Objective> objectivesList) {
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 10));
        addItems(objectivesList);
    }

    private void addItems(List<Objective> objectiveList) {
        for (Objective anObjectiveList : objectiveList) {
            Objective offsetItem = new Objective();
            offsetItem.setRemoteId(anObjectiveList.getRemoteId());
            offsetItem.setName(anObjectiveList.getName());
            objectiveMap.put(offsetItem.getRemoteId(), offsetItem);
            System.out.println("This is the remote ID: " + offsetItem.getRemoteId());
            mClusterManager.addItem(offsetItem);
        }
    }

The layout for the InfoWindow is okay, but the content is null as clickedClusterItem and clickedCluster are always null...

Any hints as to what I might be doing wrong?

I saw some other answers that involved creating a Map with the marker and the corresponding object but I am not quite sure how to do that.


Solution

  • I did this in my app. Try this in your.

        GoogleMap googleMap;    
        Double latitude;
        Double longitude;    
        Marker mark;
        MarkerOptions marker;
        Bitmap bitmap;
        String position, catName, activity;
        ToggleButton listView, mapView;
        Hashtable<Integer, String> markers;
    
        ArrayList<HomeProperty> list = new ArrayList<HomeProperty>();
        ArrayList<HomeProperty> newList;
        ArrayList<HomeProperty> category = new ArrayList<HomeProperty>();
        ArrayList<SearchProperty> searchList = new ArrayList<SearchProperty>();
        ArrayList<HomeProperty> locationList = new ArrayList<HomeProperty>();
    
        ProgressDialog dialog;
    
        ImageLoader imageLoader;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            // Introduction
            super.onCreate(savedInstanceState);
            setContentView(R.layout.map);
            initilizeMap();
            imageLoader = new ImageLoader(getApplicationContext());
    
            markers = new Hashtable<Integer, String>();
    
            listView = (ToggleButton) findViewById(R.id.listView);
            mapView = (ToggleButton) findViewById(R.id.mapView);
            listView.setOnCheckedChangeListener(this);
            mapView.setOnCheckedChangeListener(this);
            mapView.setChecked(true);
    
            // setting the arraylist containing all data of events
            ArrayData aData = (ArrayData) getIntent().getSerializableExtra("list");
            position = getIntent().getStringExtra("position");
            catName = getIntent().getStringExtra("catName");
            activity = getIntent().getStringExtra("activity");
            list = aData.getList();
    
    
            ArrayData categoryData = (ArrayData) getIntent().getSerializableExtra(
                    "category");
            category = categoryData.getList();
    
            if (getIntent().hasExtra("searchData")) {
                SearchData search = (SearchData) getIntent().getSerializableExtra(
                        "searchData");
                searchList = search.getList();
            } else {
                searchList = new ArrayList<SearchProperty>();
            }
    
            newList = new ArrayList<HomeProperty>();
            // for all categories
            if (catName.equalsIgnoreCase("All")) {
                newList.clear();
                newList.addAll(list);
    
            } else if (activity.equals("search")) {
                newList.addAll(list);
    
            }
            // for single category
            else {
                for (int count = 0; count < list.size(); count++) {
                    if (list.get(count).getCategoryName().equals(catName)) {
                        newList.add(list.get(count));
                    }
                }
            }
    
            // looping through All Transactions
            for (int count = 0; count < newList.size(); count++) {
                HomeProperty prop = new HomeProperty(newList.get(count).getLatitude(),
                        newList.get(count).getLongitude());
    
                locationList.add(prop);
            }
    
    
            for (int count = 0; count < newList.size(); count++) {
                marker = new MarkerOptions()
                        .position(
                                new LatLng(Double.parseDouble(list.get(count)
                                        .getLatitude()), Double.parseDouble(newList
                                        .get(count).getLongitude())))
                        .title(newList.get(count).getTitle())
                        .snippet(
                                new JSONMethods().SHORTIMAGEURL
                                        + newList.get(count).getImageDirectory()
                                        + "/thumb_" + newList.get(count).getImage());
    
                googleMap.addMarker(marker);
    
                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(new LatLng(Double.parseDouble(newList.get(count)
                                .getLatitude()), Double.parseDouble(newList.get(count)
                                .getLongitude()))).zoom(12).build();
    
                googleMap.animateCamera(CameraUpdateFactory
                        .newCameraPosition(cameraPosition));
                googleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
            }
    
    
            googleMap.getUiSettings().setZoomGesturesEnabled(true);
            googleMap.getUiSettings().setCompassEnabled(true);
            googleMap.getUiSettings().setMyLocationButtonEnabled(true);
            googleMap.getUiSettings().setRotateGesturesEnabled(true);
        }
    
        // set action on home button click in action bar
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    
            // Handle action bar actions click
            switch (item.getItemId()) {
            // case R.id.action_settings:
            // return true;
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
            }
        }
    
        private class CustomInfoWindowAdapter implements InfoWindowAdapter {
    
            private View view;
    
            public CustomInfoWindowAdapter() {
                view = getLayoutInflater().inflate(R.layout.custominfowindow, null);
    
            }
    
            @Override
            public View getInfoContents(Marker mark) {
    
                if (MapActivity.this.mark != null
                        && MapActivity.this.mark.isInfoWindowShown()) {
                    MapActivity.this.mark.showInfoWindow();
                }
                return view;
            }
    
            @Override
            public View getInfoWindow(final Marker mark) {
                MapActivity.this.mark = mark;
    
                final ImageView image = ((ImageView) view.findViewById(R.id.badge));
    
                imageLoader.DisplayImage(mark.getSnippet(), image);
    
                final String title = mark.getTitle();
                final TextView titleUi = ((TextView) view.findViewById(R.id.title));
                if (title != null) {
                    titleUi.setText(title);
                } else {
                    titleUi.setText("");
                }
    
                return view;
            }
    
        }