How to show InfoWindow using Android Maps Utility Library for Android - android

I'm using Google Maps Android API Utility Library in order to show several markers in a map in a clustered way.
I've followed the instructions to make it work, as well as taking a look at the examples in the library, but I can't figure out how to show an InfoWindow when a marker is clicked.
I guess getMap().setOnMarkerClickListener(mClusterManager); is the one managing the onClick events, and if commented out, I can override it using getMap().setInfoWindowAdapter(new InfoWindowAdapter() {)); but I have no access to my custom marker object.
Nevertheless, if I use getMap().setOnMarkerClickListener(mClusterManager);, I can't find a way to show the InfoWindow when a marker has been clicked.
Does anybody have any idea about how to achieve this?
Thanks a lot in advance!

You need to extend the DefaultClusterRenderer class and override the onBeforeClusterItemRendered, attaching the title to the MarkerOptions object passed as argument.
After that, you can pass your implementation to the ClusterManager.
Example:
class MyItem implements ClusterItem {
private LatLng mPosition;
private String mTitle;
public MyItem(LatLng position){
mPosition = position;
}
#Override
public LatLng getPosition() {
return mPosition;
}
public String getTitle() {
return mTitle;
}
public void setTitle(String title) {
mTitle = title;
}
}
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);
markerOptions.title(item.getTitle());
}
#Override
protected void onClusterItemRendered(MyItem clusterItem, Marker marker) {
super.onClusterItemRendered(clusterItem, marker);
//here you have access to the marker itself
}
}
And then you can use it in this way:
ClusterManager<MyItem> clusterManager = new ClusterManager<MyItem>(this, getMap());
clusterManager.setRenderer(new MyClusterRenderer(this, getMap() ,clusterManager));

Related

MarkerOptions .tag not available

I have a DefaultClusterRenderer class and my own ClusterItem, which I am using as marker on the map. I am setting the title and snippet with MarkerOptions, so I can grap them on my onInfoWindowClick event. I wanna start a new Activity, depends on which infoWindow the user clicked, but I cant add a tag to MarkerOptions, so I cant get the tag in my onInfoWindowClick method via the Marker parameter.
I am looking for a good solution to add an id to each ClusterItem and get the id via Marker in onInfoWindowClick.
public class OwnClusterRendering extends DefaultClusterRenderer<GeoPoint> {
private GoogleMap map;
public OwnClusterRendering(Context context, GoogleMap map, ClusterManager<GeoPoint> clusterManager) {
super(context, map, clusterManager);
this.map = map;
}
protected void onBeforeClusterItemRendered(GeoPoint geoPoint, MarkerOptions markerOptions) {
markerOptions.icon(geoPoint.getIcon());
markerOptions.snippet(geoPoint.getSnippet());
markerOptions.title(geoPoint.getTitle());
super.onBeforeClusterItemRendered(geoPoint, markerOptions);
}
}
MyCustomWindowAdapter:
#Override
public void onInfoWindowClick(Marker marker) {
Log.d("test", marker.getTitle());
Log.d("test", marker.getSnippet());
Log.d("test", marker.getId());
Log.d("test", String.valueOf(marker.getTag()));
// Intent intent = new Intent(context, StoneInfoSliderActivity.class);
// context.startActivity(intent);
}
Solved it with the following method from DefaultClusterRenderer<>
#Override
protected void onClusterItemRendered(GeoPoint geoPoint, Marker marker) {
marker.setTag(geoPoint.getId());
super.onClusterItemRendered(geoPoint, marker);
}

Why cluster marker icon not showing when zoom in or zoom out in android [duplicate]

This question already has answers here:
Google map marker is replaced by bounding rectangle on zoom
(3 answers)
Closed 6 years ago.
I'm trying to implement a cluster marker on my map,first, it shows me the cluster marker but when i zoom in, that expand and also show white square, i'll add some images to explain it better.
images
public class MyItem implements ClusterItem {
private final LatLng mPosition;
String Title = "";
int Icon;
public MyItem(double lat, double lng, String title, int icon) {
mPosition = new LatLng(lat, lng);
Title = title;
Icon = icon;
}
#Override
public LatLng getPosition() {
return mPosition;
}
public String getTitle() {
return Title;
}
public int getIcon() {
return Icon;
}
}
class OwnIconRendered extends DefaultClusterRenderer<MyItem> {
public OwnIconRendered() {
super(MapActivity.this, googleMap, mClusterManager);
}
#Override
protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
markerOptions.icon(BitmapDescriptorFactory.fromResource(item.getIcon()));
markerOptions.title(item.getTitle());
super.onBeforeClusterItemRendered(item, markerOptions);
}
#Override
protected void onBeforeClusterRendered(Cluster<MyItem> cluster, MarkerOptions markerOptions) {
super.onBeforeClusterRendered(cluster, markerOptions);
}
}
There is an opened issue on gmaps-api-issues, look here: https://code.google.com/p/gmaps-api-issues/issues/detail?id=9765
Also look here: Google map marker is replaced by bounding rectangle on zoom
Some workarounds are also described there.

How to get each Marker objectID when using ClusterManager

Without using the ClusterManager, I use HashMap to put the Marker and ID into the HashMap, and get the ID in the OnMarkClick method and get the data from database. It's works
markers.put(addNewMarker(geoPoint), objectId);
private Marker addNewMarker(ParseGeoPoint parseGeoPoint) {
double latitude = parseGeoPoint.getLatitude();
double longitude = parseGeoPoint.getLongitude();
return googleMap.addMarker(new MarkerOptions().position(
new LatLng(latitude, longitude)));
}
#Override
public boolean onMarkerClick(Marker marker) {
String objectId = markers.get(marker);
if (null == objectId) {
return false;
}
getMemoryBriefInfo(objectId);
return true;
}
But now I need to use the ClusterManager to cluster multiple markers into number.
The problems is It seems there is no way to implement this, in the demo of Google, it just add the Items into the Cluster.
There is a OnMarkerClick method in the ClusterManager class, but I don't how to Override this and set with my own unique ID.
there is a global solution for you that help to add title, snippet and icon so you can get what you want.
Modify your ClusterItem Object and add 3 variables :
public class MyItem implements ClusterItem {
private final LatLng mPosition;
BitmapDescriptor icon;
String title;
String snippet;
public MyItem(BitmapDescriptor ic,Double lat , Double lng,String tit ,String sni)
{
mPosition = new LatLng(lat,lng);
icon = ic;
title = tit;
snippet = sni;
}
And after you create your costume render :
public class OwnRendring extends DefaultClusterRenderer<MyItem> {
public OwnRendring(Context context, GoogleMap map,
ClusterManager<MyItem> clusterManager) {
super(context, map, clusterManager);
}
protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
markerOptions.icon(item.getIcon());
markerOptions.snippet(item.getSnippet());
markerOptions.title(item.getTitle());
super.onBeforeClusterItemRendered(item, markerOptions);
}
}
After that just put this line in your SetUpCluster() function before addItems():
mClusterManager.setRenderer(new OwnRendring(getApplicationContext(),mMap,mClusterManager));

How to make markers clickable and clusters not clickable? [duplicate]

This question already has answers here:
Android Maps Utils Clustering show InfoWindow
(3 answers)
Closed 4 years ago.
i'm developing my app that use markers on google map. Then i use clustering to gather markers that are too closer. Here is part of my code:
MapViewer.java
public class MapViewer extends Activity implements OnInfoWindowClickListener {
private GoogleMap map;
private ClusterManager<MyItem> mClusterManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapviewer);
try {
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
if (map != null) {
map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.getUiSettings().setRotateGesturesEnabled(false);
mClusterManager = new ClusterManager<MyItem>(this, map);
mClusterManager.setRenderer(new MyClusterRenderer(this, map, mClusterManager));
map.setOnCameraChangeListener(mClusterManager);
map.setOnMarkerClickListener(mClusterManager);
map.setInfoWindowAdapter(new ClusterInfoWindow(getLayoutInflater()));
map.setOnInfoWindowClickListener(this);
addItems();
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
#Override
public void onInfoWindowClick(Marker marker) {
// My code
}
public void addItems() {
// My code
}
}
ClusterInfoWindow.java
public class ClusterInfoWindow implements InfoWindowAdapter {
LayoutInflater inflater = null;
public ClusterInfoWindow(LayoutInflater inflater) {
this.inflater = inflater;
}
#Override
public View getInfoWindow(Marker marker) {
return(null);
}
#Override
public View getInfoContents(Marker marker) {
View popup = inflater.inflate(R.layout.infowindow, null);
TextView tv = (TextView)popup.findViewById(R.id.title);
tv.setText(marker.getTitle());
tv = (TextView)popup.findViewById(R.id.snippet);
tv.setText(marker.getSnippet());
return(popup);
}
}
If i click on single marker it shows a popup with some details. The same happens if i click on a cluster. How to make the cluster not clickable? I don't want to show anything when user click on cluster.
Thank you.
Based on the documentation from Google Maps:
If you want to add specific functionality in response to a marker click event, set the map's OnMarkerClickListener() to the ClusterManager, since ClusterManager implements the listener.
So, instead of doing map.setOnMarkerClickListener(mClusterManager); you can change it to the following:
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
marker.showInfoWindow();
return false;
}
});
You want to implement ClusterManager.OnClusterItemClickListener or ClusterManager.OnClusterItemInfoWindowClickListener

Showing custom InfoWindow for Android Maps Utility Library for Android

I'm using the library Google Maps Utility for Android which allows to create clustering int he maps and I need to show a custom InfoWindow but I can't find any method to do this.
In order to show the info window, I have the following class, and in the method onClusterItemRendered is where I have access to the info of the marker:
class MyClusterRenderer extends DefaultClusterRenderer<MarkerItem> {
public MyClusterRenderer(Context context, GoogleMap map,
ClusterManager<MarkerItem> clusterManager) {
super(context, map, clusterManager);
}
#Override
protected void onBeforeClusterItemRendered(MarkerItem item,
MarkerOptions markerOptions) {
super.onBeforeClusterItemRendered(item, markerOptions);
markerOptions.title(String.valueOf(item.getMarkerId()));
}
#Override
protected void onClusterItemRendered(MarkerItem clusterItem,
Marker marker) {
super.onClusterItemRendered(clusterItem, marker);
}
}
Is there anybody who has used the library and knows how to show a custom InfoWindow such as the way it was used in the Google Maps? Like:
getMap().setInfoWindowAdapter(new InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker arg0) {
return null;
}
#Override
public View getInfoContents(Marker arg0) {
return null;
}
});
Yes, this can be done. ClusterManager maintains two MarkerManager.Collections:
one for cluster markers, and
one for individual item markers
You can set a custom InfoWindowAdapter for each of these kinds of markers independently.
Implementation
First, install your ClusterManager's MarkerManager as the map's InfoWindowAdapter:
ClusterManager<MarkerItem> clusterMgr = new ClusterManager<MarkerItem>(context, map);
map.setInfoWindowAdapter(clusterMgr.getMarkerManager());
Next, install your custom InfoWindowAdapter as the adapter for one or both of the marker collections:
clusterMgr.getClusterMarkerCollection().setOnInfoWindowAdapter(new MyCustomAdapterForClusters());
clusterMgr.getMarkerCollection().setOnInfoWindowAdapter(new MyCustomAdapterForItems());
The final piece is mapping the raw Marker object that you'll receive in your custom InfoWindowAdapter's callback to the ClusterItem object(s) that you added to the map in the first place. This can be achieved using the onClusterClick and onClusterItemClick listeners, as follows:
map.setOnMarkerClickListener(clusterMgr);
clusterMgr.setOnClusterClickListener(new OnClusterClickListener<MarkerItem>() {
#Override
public boolean onClusterClick(Cluster<MarkerItem> cluster) {
clickedCluster = cluster; // remember for use later in the Adapter
return false;
}
});
clusterMgr.setOnClusterItemClickListener(new OnClusterItemClickListener<MarkerItem>() {
#Override
public boolean onClusterItemClick(MarkerItem item) {
clickedClusterItem = item;
return false;
}
});
Now you have everything you need to assemble your custom InfoWindow content in your respective Adapters! For example:
class MyCustomAdapterForClusters implements InfoWindowAdapter {
#Override
public View getInfoContents(Marker marker) {
if (clickedCluster != null) {
for (MarkerItem item : clickedCluster.getItems()) {
// Extract data from each item in the cluster as needed
}
}
// build your custom view
// ...
return view;
}
}
So basically you have your class starting like this:
private class MarkerItemClusterRenderer extends
DefaultClusterRenderer<MarkerItem> {
public MarkerItemClusterRenderer(Context context, GoogleMap map,
ClusterManager<MarkerItem> clusterManager) {
...
So in that parameter map of the type GoogleMap is where you have to specify your setInfoWindowAdapter.

Categories

Resources