android - Can I add a marker to an ArrayList and a HashMap at the same time? -


i have code working want. mean have map fragment displays 5 marker category, can filter them want category , works fine. might observe marker 1 , marker 8 appear in more 1 category. here code , continue problem afterwards:

        boolean msetcameraposition;         boolean checkbox1checked, checkbox2checked, checkbox3checked,     checkbox4checked, checkbox5checked;         private int maptypeselected;         checkbox cballday, cbbefore12, cbbetween1216, cbbetween1620, ccbafter20;         alertdialog dialog;         list<marker> firstcategorylist = new arraylist<>();         list<marker> secondcategorylist = new arraylist<>();         list<marker> thirdcategorylist = new arraylist<>();         list<marker> fourthcategorylist = new arraylist<>();         list<marker> fifthcategorylist = new arraylist<>();    @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);           if (android.os.build.version.sdk_int >= build.version_codes.m) {             checklocationpermission();         }           supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map);         mapfragment.getmapasync(this);           if (savedinstancestate == null) {             maptypeselected = googlemap.map_type_normal;             msetcameraposition = true;          } else {             maptypeselected = savedinstancestate.getint("the_map_type", googlemap.map_type_normal);             msetcameraposition = false;      @override     public void onmapready(googlemap googlemap) {          mmap = googlemap;         if (msetcameraposition) {             initiallocation(toulouse_lat, toulouse_lng, 12);          }         mmap.setmaptype(maptypeselected);   if (initialmarkers) {             addmarkers2map();         }    public void addmarkers2map() {          // markers location          latlng marker1 = new latlng(43.607044, 1.450307);         latlng marker2= new latlng(43.571505, 1.417759);         latlng marker3= new latlng(43.607469, 1.447162);         latlng marker4= new latlng(43.600723, 1.455917);         latlng marker5= new latlng(43.604892, 1.476562);         latlng marker6= new latlng(43.604496, 1.474924);         latlng marker7= new latlng(43.604781, 1.474502);          // markers day long         firstcategorylist.add(mmap.addmarker(new markeroptions().position(marker1).title("first place ").icon(bitmapdescriptorfactory.fromresource(r.drawable.beer_marker)).snippet("hh: 8.30 - 22.30")));          // markers before 12 pm         secondcategorylist.add(mmap.addmarker(new markeroptions().position(marker2).title("second place").icon(bitmapdescriptorfactory.fromresource(r.drawable.beer_marker)).snippet("hh: 10.30 - 11.30")));          // markers between 12-16         thirdcategorylist.add(mmap.addmarker(new markeroptions().position(marker3).title("third place").icon(bitmapdescriptorfactory.fromresource(r.drawable.beer_marker)).snippet("hh: 15.30 - 16.30")));         thirdcategorylist.add(mmap.addmarker(new markeroptions().position(marker4).title("fourth place").icon(bitmapdescriptorfactory.fromresource(r.drawable.beer_marker)).snippet("hh: 15.00 - 16.00")));          // markers between 16-20         fourthcategorylist.add(mmap.addmarker(new markeroptions().position(marker5).title("fifth place").icon(bitmapdescriptorfactory.fromresource(r.drawable.beer_marker)).snippet("hh: 15.30 - 16.30")));         fourthcategorylist.add(mmap.addmarker(new markeroptions().position(marker6).title("sixth place").icon(bitmapdescriptorfactory.fromresource(r.drawable.beer_marker)).snippet("hh: 16.30 - 17.30")));         fourthcategorylist.add(mmap.addmarker(new markeroptions().position(marker7).title("seventh place").icon(bitmapdescriptorfactory.fromresource(r.drawable.beer_marker)).snippet("hh: 18.30 - 19.30")));         fourthcategorylist.add(mmap.addmarker(new markeroptions().position(marker8).title("eighth place").icon(bitmapdescriptorfactory.fromresource(r.drawable.beer_marker)).snippet("hh: 17.30 - 18.30\nhh:21.30 - 22.30")));           // markers after 20         fifthcategorylist.add(mmap.addmarker(new markeroptions().position(marker8).title("eighth place").icon(bitmapdescriptorfactory.fromresource(r.drawable.beer_marker)).snippet("hh: 17.30 - 18.30\nhh:21.30 - 22.30")));         fifthcategorylist.add(mmap.addmarker(new markeroptions().position(marker1).title("first place").icon(bitmapdescriptorfactory.fromresource(r.drawable.beer_marker)).snippet("hh: 8.30 - 22.30")));     public void filterthemarkers(view view) {           if (dialog == null){         alertdialog.builder builder;         builder = new alertdialog.builder(this);         layoutinflater inflater = this.getlayoutinflater();         @suppresslint("inflateparams") view checkboxview = inflater.inflate(r.layout.markers_filtering, null);         builder.setview(checkboxview);         cballday = (checkbox) checkboxview.findviewbyid(r.id.checkbox1);         if (checkbox1checked != null) {             cballday.setchecked(checkbox1checked);          }         cbbefore12 = (checkbox) checkboxview.findviewbyid(r.id.checkbox2);         if (checkbox2checked != null) {             cbbefore12.setchecked(checkbox2checked);          }         cbbetween1216 = (checkbox) checkboxview.findviewbyid(r.id.checkbox3);         if (checkbox3checked != null) {             cbbetween1216.setchecked(checkbox3checked);          }         cbbetween1620 = (checkbox) checkboxview.findviewbyid(r.id.checkbox4);         if (checkbox4checked != null) {             cbbetween1620.setchecked(checkbox4checked);          }         ccbafter20 = (checkbox) checkboxview.findviewbyid(r.id.checkbox5);         if (checkbox5checked != null) {             ccbafter20.setchecked(checkbox5checked);          }          dialog = builder.create();       }          dialog.show();     }    public void displayselectedmarkers(view view) {          dialog.dismiss();         log.i("tag", "all day " + cballday.ischecked() + " before 12 " + cbbefore12.ischecked() + " between 12-16 " + cbbetween1216.ischecked() + " between 16-20" + cbbetween1620.ischecked() + " after 20 " + ccbafter20.ischecked());         //according these check boxes status execute code show/hide markers           if (cballday.ischecked() && cbbefore12.ischecked() && cbbetween1216.ischecked() && cbbetween1620.ischecked() && ccbafter20.ischecked()) {             // show markers             (marker marker : firstcategorylist) {                 marker.setvisible(true);             }             (marker marker : secondcategorylist) {                 marker.setvisible(true);             }             (marker marker : thirdcategorylist) {                 marker.setvisible(true);             }             (marker marker : fourthcategorylist) {                 marker.setvisible(true);             }             (marker marker : fifthcategorylist) {                 marker.setvisible(true);             }         } else if (cballday.ischecked() && !cbbefore12.ischecked() && !cbbetween1216.ischecked() && !cbbetween1620.ischecked() && !ccbafter20.ischecked()) {             // show day markers             (marker marker : firstcategorylist) {                 marker.setvisible(true);             }             (marker marker : secondcategorylist) {                 marker.setvisible(false);             }             (marker marker : thirdcategorylist) {                 marker.setvisible(false);             }             (marker marker : fourthcategorylist) {                 marker.setvisible(false);             }             (marker marker : fifthcategorylist) {                 marker.setvisible(false);             }         } //....and goes while until finish possibilities 

now want open new activity every markers info window clicked solution found add markers hashmap , give name every marker can add hashmap , arraylist, this:

private map<marker, class> allmarkersmap = new hashmap<>(); marker markermarker1 = mmap.addmarker(new markeroptions().position(marker1).title("bar acasa").icon(bitmapdescriptorfactory.fromresource(r.drawable.beer_marker)).snippet("hh: 8.30 - 22.30"));  firstcategorylist.add(markermarker1); allmarkersmap.put(markermarker1, marker1.class); 

now on onmapready adding code , create class every marker marker1.java, marker2.java.....:

mmap.setoninfowindowclicklistener(myoninfowindowclicklistener); googlemap.oninfowindowclicklistener myoninfowindowclicklistener = new googlemap.oninfowindowclicklistener() {     @override     public void oninfowindowclick(marker marker) {         class cls = allmarkersmap.get(marker);         intent intent = new intent(mainactivity.this, cls );         startactivity(intent);     } }; 

everything works fine markers appear in 1 category, ones in more 1 category filter not working anymore, displaying markers last added hashmap(hopefully understand saying here).

so, can , still have filter functioning? imagining happening fact once define marker , put in arraylist moves marker there, , afterwards when put in hashmap moves again hashmap when filter arraylists marker not in list anymore...
best solution info window opening different activity each marker displayed?

i won't write code solution because think better write , think idea behind more important code itself.

first of all, small suggestion you, whenever start developing ui things display information, try find generic description or rule describes information displayed. when found rule/description means found model class. suggest use custom classes created hold information required ui.

now example, understand, have markers added category , based on category display them on map. would create custom class, called let's myawesomemarker has marker member , arraylist<string> category.

something this:

public class myawesomemarker {    private marker mmarker;    private list<string> mmarkercategories; // here can mark if marker in more 1 categories.    // constructors    public myawesomemarker(marker marker, string category) {      mmarker = marker;      mmarkercategories = new arraylist<string>();      mmarkercategories.add(category);                }     public void addnewcategory(string category) {       if(category != null && !mmarkercategorie.contains(category)) {          mmarkercategories.add(category);       }    }     public void togglemarkerifhascategory(string thecategory) {        for(string category : mmarkercategories) {            if(category.equals(thecategory) {                    marker.setvisible(true);            }            else {                  marker.setvisible(false);            }         }    }     // setters getters  } 

then in main class have list of myawesomemarker, let's call mymarkers

now want display markers in category named awesomness.

for(myawesomemarker marker : mymarkers) {        marker.togglemarkerifhascategory("awesomness"); } 

now thing have do, generate lit of custom markers , you're done.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -