Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add info window for clustering marker in android?

I would like to show an info window with an image for all markers in Android. I referred to this Google Map Cluster Tutorial.

like image 211
Deva Avatar asked Sep 22 '14 07:09

Deva


1 Answers

MyItem Class:

import com.google.android.gms.maps.model.LatLng;
import com.google.maps.android.clustering.ClusterItem;

public class MyItem implements ClusterItem {

    private LatLng mPosition;
    private String mLatitude = "";
    private String mStoreLogo = "";
    private String mLongitude = "";

    @Override
    public LatLng getPosition() {
        return mPosition;
    }

    public void setPosition(LatLng mPosition) {
        this.mPosition = mPosition;
    }

    public LatLng getmPosition() {
        return mPosition;
    }

    public void setmPosition(LatLng mPosition) {
        this.mPosition = mPosition;
    }

    public String getmLatitude() {
        return mLatitude;
    }

    public void setmLatitude(String mLatitude) {
        this.mLatitude = mLatitude;
    }

    public String getmLongitude() {
        return mLongitude;
    }

    public void setmLongitude(String mLongitude) {
        this.mLongitude = mLongitude;
    }

    public String getmStoreLogo() {
        return mStoreLogo;
    }

    public void setmStoreLogo(String mStoreLogo) {
        this.mStoreLogo = mStoreLogo;
    }

}

Map Activity Class:

public class Map extends FragmentActivity implements
        ClusterManager.OnClusterClickListener<MyItem>,
        ClusterManager.OnClusterInfoWindowClickListener<MyItem>,
        ClusterManager.OnClusterItemClickListener<MyItem>,
        ClusterManager.OnClusterItemInfoWindowClickListener<MyItem> {


    private ClusterManager<MyItem> mClusterManager;
    private Cluster<MyItem> clickedCluster;
    private MyItem clickedClusterItem;


    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.map_view);

        try {
          // Initializing Map from XML :
            GooglePlayServicesUtil.isGooglePlayServicesAvailable(Map.this);
            SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.mMapView);

            mMapView = mapFrag.getMap();
            mMapView.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            mMapView.getUiSettings().setZoomControlsEnabled(true);
            mMapView.getUiSettings().setCompassEnabled(true);
            mMapView.getUiSettings().setMyLocationButtonEnabled(true);
            mMapView.getUiSettings().setRotateGesturesEnabled(true);
            mMapView.getUiSettings().setScrollGesturesEnabled(true);
            mMapView.getUiSettings().setTiltGesturesEnabled(true);
            mMapView.getUiSettings().setZoomGesturesEnabled(true);
            mMapView.setMyLocationEnabled(true);

        } catch (Exception e) {
            mMapFrame.setVisibility(View.GONE);
            Utils.displayToast("Your device doesn't support Google Map", Map.this);
        }

   // Creating cluster manager object.

    mClusterManager = new ClusterManager<MyItem>(Map.this, mMapView);
        mMapView.setOnCameraChangeListener(mClusterManager);
        mClusterManager.setRenderer(new MyClusterRenderer(Map.this, mMapView,
                mClusterManager));

        mMapView.setOnInfoWindowClickListener(mClusterManager);
        mMapView.setInfoWindowAdapter(mClusterManager.getMarkerManager());
        mClusterManager.getClusterMarkerCollection().setOnInfoWindowAdapter(
                new MyCustomAdapterForClusters());
        mClusterManager.getMarkerCollection().setOnInfoWindowAdapter(
                new MyCustomAdapterForItems());
        mMapView.setOnMarkerClickListener(mClusterManager);
        mClusterManager.setOnClusterClickListener(this);
        mClusterManager.setOnClusterInfoWindowClickListener(this);
        mClusterManager.setOnClusterItemClickListener(this);
        mClusterManager.setOnClusterItemInfoWindowClickListener(this);

        mClusterManager
                .setOnClusterClickListener(new OnClusterClickListener<MyItem>() {
                    @Override
                    public boolean onClusterClick(Cluster<MyItem> cluster) {
                        clickedCluster = cluster;
                        return false;
                    }
                });

        mClusterManager
                .setOnClusterItemClickListener(new OnClusterItemClickListener<MyItem>() {
                    @Override
                    public boolean onClusterItemClick(MyItem item) {
                        clickedClusterItem = item;
                        return false;
                    }
                });

        // Adding Objects to the Cluster.    

        mClusterManager.addItem(mItemData);
                        mMapView.animateCamera(CameraUpdateFactory
                                .newLatLngZoom(mLatLng, 7));
        mClusterManager.cluster();
    }

    class MyClusterRenderer extends DefaultClusterRenderer<MyItem> {

        public MyClusterRenderer(Context context, GoogleMap map,
                ClusterManager<MyItem> clusterManager) {
            super(context, map, clusterManager);
        }

        @Override
        protected void onBeforeClusterItemRendered(MyItem item,
                MarkerOptions markerOptions) {
            super.onBeforeClusterItemRendered(item, markerOptions);
        }

        @Override
        protected void onClusterItemRendered(MyItem clusterItem, Marker marker) {
            super.onClusterItemRendered(clusterItem, marker);
        }

    }

    // Custom adapter info view :
    public class MyCustomAdapterForItems implements InfoWindowAdapter {

        private final View myContentsView;

        MyCustomAdapterForItems() {
            myContentsView = getLayoutInflater().inflate(
                    R.layout.map_info_window_dialog, null);
        }

        @Override
        public View getInfoContents(Marker marker) {
            return null;
        }

        @Override
        public View getInfoWindow(Marker marker) {
            TextView tvTitle = ((TextView) myContentsView
                    .findViewById(R.id.txtHeader));
            TextView tvSnippet = ((TextView) myContentsView
                    .findViewById(R.id.txtAddress));

            tvTitle.setTypeface(mTyFaceKreonBold);
            tvSnippet.setTypeface(mTyFaceKreonBold);
            if (clickedClusterItem != null) {
                tvTitle.setText(clickedClusterItem.getmStoreName());
                tvSnippet.setText(clickedClusterItem.getmAddressOne());
            }
            return myContentsView;
        }
    }

     // class for Main Clusters.    
    public class MyCustomAdapterForClusters implements InfoWindowAdapter {

        private final View myContentsView;

        MyCustomAdapterForClusters() {
            myContentsView = getLayoutInflater().inflate(
                    R.layout.map_info_window_dialog, null);
        }

        @Override
        public View getInfoContents(Marker marker) {
            return null;
        }

        @Override
        public View getInfoWindow(Marker marker) {
            TextView tvTitle = ((TextView) myContentsView
                    .findViewById(R.id.txtHeader));
            TextView tvSnippet = ((TextView) myContentsView
                    .findViewById(R.id.txtAddress));
            tvSnippet.setVisibility(View.GONE);
            tvTitle.setTypeface(mTyFaceKreonBold);
            tvSnippet.setTypeface(mTyFaceKreonBold);

            if (clickedCluster != null) {
                tvTitle.setText(String
                        .valueOf(clickedCluster.getItems().size())
                        + " more offers available");
            }
            return myContentsView;
        }
    }

    @Override
    public void onClusterItemInfoWindowClick(MyItem item) {
        Intent intent = new Intent(Map.this,NextActivity.class);
        intent.putExtra("mLatitude", item.getmLatitude());
        intent.putExtra("mLongitude", item.getmLongitude());

        startActivity(intent);
        finish();
    }

    @Override
    public boolean onClusterItemClick(MyItem item) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onClusterInfoWindowClick(Cluster<MyItem> cluster) {
        // TODO Auto-generated method stub
    }

    @Override
    public boolean onClusterClick(Cluster<MyItem> cluster) {
        // TODO Auto-generated method stub
        return false;
    }
}

The code is self explanatory. Please add the cluster library and google play services lib to your build path. Please let me know if you have any queries.

like image 122
Rethinavel Avatar answered Sep 23 '22 14:09

Rethinavel