Clustering not working in google maps for android. What am i doing wrong? -
here code render 100 markers around given location, no matter do, clusters/markers wont show up. have doubt if issue initialization of clustermanager, mclustermanager = new clustermanager<markermanager>(getactivity(), mmap);
from example google shown here,clustermanager
initialized mclustermanager = new clustermanager<myitem>(this, getmap());
but class inherits fragment
, getactivity
return wrong context? think expects com.example.mkallingal.mapapp3.mainactivity.placeholderfragment
your appreciated , mark best answer if me. thank you.
below code:
public static class placeholderfragment extends fragment { public googlemap mmap; private clustermanager<markermanager> mclustermanager; context _mapcontext; private void setupmapifneeded(view rootview) { // null check confirm have not instantiated map. if (mmap == null) { fragmentactivity activity = (fragmentactivity)rootview.getcontext(); fragmentmanager manager = activity.getsupportfragmentmanager(); fragment x= manager.findfragmentbyid(r.id.container); fragment _childfragment= x.getchildfragmentmanager().findfragmentbyid(r.id.map); // try obtain map supportmapfragment. mmap = ((supportmapfragment) _childfragment) .getmap(); // check if successful in obtaining map. if (mmap != null) { clusterup(5.293842, 101.682636); } } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); _mapcontext= container.getcontext(); setupmapifneeded(rootview); return rootview; } private void clusterup(double lat, double lng) { latlng sydney = new latlng(5.415694, 101.678282); latlng mountain_view = new latlng(lat,lng); mmap.movecamera(cameraupdatefactory.newlatlngzoom(sydney, 15)); mmap.animatecamera(cameraupdatefactory.zoomin()); mmap.animatecamera(cameraupdatefactory.zoomto(10), 2000, null); cameraposition cameraposition = new cameraposition.builder() .target(mountain_view) // sets center of map mountain view .build(); // creates cameraposition builder mmap.animatecamera(cameraupdatefactory.newcameraposition(cameraposition)); mmap.setoncamerachangelistener(mclustermanager); mmap.setonmarkerclicklistener(mclustermanager); mclustermanager = new clustermanager<markermanager>(getactivity(), mmap); (int i=0;i<100;i++){ latlng _newlocation= getlocationnearcords(5.415694, 101.678282, 1000); //randomly generate 100 cordinates around location markermanager offsetitem = new markermanager(_newlocation.latitude,_newlocation.longitude); mclustermanager.additem(offsetitem); } } public class markermanager implements clusteritem { private latlng mposition = new latlng(22.2222, 33.3333); public markermanager() { } public markermanager(double lat, double lng) { mposition = new latlng(lat, lng); } @override public latlng getposition() { return mposition; } } }
whoops, bad, event handlers attached before initialization of clustermanager, should like,
mclustermanager = new clustermanager<markermanager>(getactivity(), mmap); mmap.setoncamerachangelistener(mclustermanager); mmap.setonmarkerclicklistener(mclustermanager);
now works...
Comments
Post a Comment