I would like to show an info window with an image for all markers in Android. I referred to this Google Map Cluster Tutorial.
Asked
Active
Viewed 1.1k times
6
-
i have no idea to add custom info window using the cluster library u have any idea – Deva Sep 22 '14 at 07:30
1 Answers
30
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.

fawaad
- 341
- 6
- 12

Rethinavel
- 3,912
- 7
- 28
- 49
-
-
private void readItems() throws JSONException { // InputStream inputStream = getResources().openRawResource(R.raw.radar_search); // List
items = new MyItemReader().read(inputStream); Log.e("items size", ""+items.size()); Log.e("items", ""+items); mClusterManager.addItems(items); map.animateCamera(CameraUpdateFactory .newLatLngZoom(items.get(0).getPosition(), 7)); mClusterManager.cluster(); } – Deva Sep 22 '14 at 09:45 -
adding list of latlng in a list and then parse the list to this method..after that added your coding.. – Deva Sep 22 '14 at 09:47
-
@Deva thats it. It will work. Do you get any exceptions? Any error message? – Rethinavel Sep 22 '14 at 09:50
-
Did you call the window adapter like this? `mClusterManager.getMarkerCollection().setOnInfoWindowAdapter( new MyCustomAdapterForItems());` and `mClusterManager.setOnClusterItemInfoWindowClickListener(this); ` Can i see your complete code. Please update that in your question. – Rethinavel Sep 22 '14 at 09:54
-
You want to show cluster info dialog or cluster item info dialog? It's working very fine to me. Please show me your code. – Rethinavel Sep 22 '14 at 09:57
-
how to remove the plus symbol in cluster show exact count and when click the cluster show all data in info window..... – Deva Sep 23 '14 at 04:13
-
@Deva I didn't dig that much, will do if time allows. If you are suppose to do that. Please let me know. Let me learn from you. – Rethinavel Sep 25 '14 at 09:46
-