google maps - android GoogleMaps marker scalable icon -
i using googleplaces api populate things around user, works fine now. use icon returned api set each marker. it's (except icon ugly well), problem becomes over-stacked whenever zoom-out (around zoom level 13) tried implement hide marker whenever zoom-out much, or tried find math formula change alpha property of marker smoothly hide more zoom-out solutions not pleasant.
map.setoncamerachangelistener(new googlemap.oncamerachangelistener() { @override public void oncamerachange(cameraposition cameraposition) { float zoomlevel = cameraposition.zoom; if (zoomlevel < 13) { (int = 0; < places.size(); i++) places.get(i).setvisible(false); } else { (int = 0; < places.size(); i++) { places.get(i).setvisible(true); } } } });
is there way handle such case ?
i have 1 example : airbnb application (but don't understand perform behind scene).
it seems airbnb hides/shows specific markers when zoom level changes.
you use marker clustering markers join single group when zoomed out. if that's not want, read on.
i answered similar question regarding polylines here.
the solution create sparse array, specific zoom levels, contains specific markers. e.g.:
// zoom level 16 , list<marker> markers = new arraylist<marker>(); markers.add(mmap.addmarker(new markeroptions() .position(new latlng(48.867068, 2.351503)))); ... // markers hidden default (marker marker : markers) marker.setvisible(false); mzoommarkers.put(16, markers);
then initialize map, revealing of markers:
sparsearray<list<marker>> mzoommarkers = new sparsearray<list<marker>>(); int mzoomlevel = 16; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); ... // zoom 16 mmap.animatecamera(cameraupdatefactory.newlatlngzoom(point, mzoomlevel), 1000, null); // reveal markers for(int i=0; i<mzoommarkers.size(); i++) { // loop until zoom level reached if (mzoommarkers.keyat(i) > zoomlevel) break; showmarkers(mzoommarkers.get(mzoommarkers.keyat(i))); } ... } private void showmarkers(list<marker> markers) { if (markers != null) { (marker marker : markers) marker.setvisible(true); } } private void hidemarkers(list<marker> markers) { if (markers != null) { (marker marker : markers) marker.setvisible(false); } }
finally set zoom level listener add/remove markers accordingly:
mmap.setoncamerachangelistener(new googlemap.oncamerachangelistener() { @override public void oncamerachange(cameraposition cameraposition) { /* note because casting zoomlevel int, * considered changed when reaches * new integer when rounded (e.g. 5.0, 6.0 etc.) */ int newzoomlevel = (int) cameraposition.zoom; if (newzoomlevel != mzoomlevel) { log.d(tag, "new zoom level: " + newzoomlevel); // loop changed zoom levels // e.g. zoomed-out 15 13, hide [14, 15] if (newzoomlevel < mzoomlevel) { // zoomed out (int i=1; i<=mzoomlevel-newzoomlevel; i++) hidemarkers(mzoommarkers.get(newzoomlevel + i)); } else { // zoomed-in (int i=1; i<=newzoomlevel-mzoomlevel; i++) showmarkers(mzoommarkers.get(mzoomlevel + i)); } mzoomlevel = newzoomlevel; } } });
Comments
Post a Comment