java - How to create a Singleton method which adds a Google Map marker? -
how create method can add marker singleton. every time try, the map null , marker null. doesn't give me nullpointer exception 0.
how make them not null marker can show on map after use call controller.getinstance().adaugaruta(mapsactivity.this);
on main activity , use map<> objectmap method. problem here, on singleton...i think, because on main activity receive map<> objectmap key entered value 0 because marker null.
public class controller { googlemap mmap; private static controller instance; private controller() { } public static controller getinstance() { if (instance == null) { instance = new controller(); } return instance; } public map<string, list<marker>> adaugaruta(showtherute theroute) { map<string, list<marker>> objectmap = new hashmap<>(); marker mae = null; icongenerator iconita = new icongenerator(mapsactivity.getappcontext()); iconita.setcontentpadding(5, -5, 5, -5); iconita.setrotation(-270); iconita.setcontentrotation(270); bitmap iconbit = iconita.makeicon("this icon."); if (mmap != null) { here problem --->mae = mmap.addmarker(new markeroptions().icon(bitmapdescriptorfactory.frombitmap(iconbit)).position(new latlng(46.762035, 23.597659)).anchor(0f, 0.5f)); mae.isvisible(); } list<marker> marker1 = new arraylist<>(); marker1.add(mae); objectmap.put("marker1", marker1); theroute.gettherute(objectmap); return objectmap; }
the thing if remove if (mmap != null)
gives nullpointerexception. use interface can use data map<> objectmap in mapsactivity implements interface. works, key , value 0 because marker null.
in code didn't define >>>> mmap <<<<<
of course should give null pointer exception !!!
define map somewhere, can this:
googlemap mmap; private static controller instance; private controller() { } public static controller getinstance(googlemap map) { if (instance == null) { instance = new controller(); instance.mmap=map; } return instance; }
and later in other function :
public map<string, list<marker>> adaugaruta(showtherute theroute) { ... if (instance.mmap != null) { mae = instance.mmap.addmarker(new markeroptions().icon(bitmapdescriptorfactory.frombitmap(iconbit)).position(new latlng(46.762035, 23.597659)).anchor(0f, 0.5f)); mae.isvisible(); } .. return objectmap; }
Comments
Post a Comment