google maps - How to add info window for clustering marker in android? -
i referred this google map android . want show info window image markers in android.
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; } }
youractivity 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) { // todo auto-generated method stub super.oncreate(arg0); setcontentview(r.layout.map_view); try { // initializing map 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 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) { // todo auto-generated method stub 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 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) { // todo auto-generated method stub 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) { // todo auto-generated method stub 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 self explanatory. hooe had added cluster library , google play services lib build path. please let me know if have queries.
Comments
Post a Comment