android - Updating Marker in google map -
i using google map in app. storing lat-long , test type in database. retrieving value database , showing marker on google map. using 2 different image test type. if test type manual using red image if test type auto using blue image.
here code retrieve value , showing on google map
public void setpinonmap(){ try{ cursor pinrecordcursor = mtestpinrecordhandler.getpinfromdatabase(); mpinmarkerlist = new arraylist<testpinmarker>(); if(pinrecordcursor != null && pinrecordcursor.movetofirst()) { {//string testname, string testtype, string latitude, string longitude testpinmarker markers = new testpinmarker(pinrecordcursor.getstring(1),pinrecordcursor.getstring(2), pinrecordcursor.getstring(3),pinrecordcursor.getstring(4)); mpinmarkerlist.add(markers); }while(pinrecordcursor.movetonext()); pinrecordcursor.close(); (testpinmarker mm : mpinmarkerlist) { mmap.addmarker(mm.getmarkeroption()); } thread.sleep(300); } } catch(exception e) { mlogger.printlog("pin", "exception in maphandler-->setpinonmap"); mlogger.printlog("pin", e.getmessage()); } }
i calling method in oncreate() method , update value every time using handler
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.d("map","coming inside on oncreate()"); mtestpinrecordhandler = new testpinrecordhandler(getapplicationcontext()); mlogger = new logger(); setcontentview(r.layout.activity_map_sample); try{ mplayservicestatus = checkgoogleplayservicesavailability(); } catch(exception e) { system.out.println("kindly update google play service"); } string lpreviouszoomlevelstring = config.getsetting(getapplicationcontext(), "mapzoomlevel"); if(lpreviouszoomlevelstring == null || lpreviouszoomlevelstring.length() == 0) { config.setsetting(getapplicationcontext(), "mapzoomlevel", float.tostring(mpreviouszoomlevel)); } if(mplayservicestatus) mmap = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.mapview)).getmap(); // mmap.setmylocationenabled(true); log.d("map","coming inside on oncreate()"); /**add pin map */ setpinonmap(); mhandler = new handler(); if(mplayservicestatus){ mhandler.removecallbacks(updatecentertask); mhandler.postdelayed(updatecentertask, 100);
this handler code
private runnable updatecentertask = new runnable(){ @override public void run() { // todo auto-generated method stub try{ if(mmap==null) mmap = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.mapview)).getmap(); try{ gpslistener.uselastknownlocation(); } catch(exception e) { e.printstacktrace(); } log.d("map","coming inside map update task"); setpinonmap(); mcurrentlattitude = mgpslistener.getlatituderaw(); mcurrentlongitude = mgpslistener.getlongituderaw(); log.d("map","lat-lang values: "+mcurrentlattitude +" : "+mcurrentlongitude); latlng coordinates = new latlng(mcurrentlattitude, mcurrentlongitude); cameraupdate center= cameraupdatefactory.newlatlng(coordinates); mmap.movecamera(center); string lpreviouszoomlevelstring = config.getsetting(getapplicationcontext(), "mapzoomlevel"); mpreviouszoomlevel = float.parsefloat(lpreviouszoomlevelstring); log.d("map","mpreviouszoomlevel value: "+ mpreviouszoomlevel); cameraupdate zoom=cameraupdatefactory.zoomto(mpreviouszoomlevel); mmap.animatecamera(zoom); } catch(exception e) { e.printstacktrace(); } { /** thread called after delay period **/ mhandler.postdelayed(updatecentertask, 5000); } }};
i able show marker problem if perform test come mapactivity update value every time updated in databases. if perform same process on mapactivity. not update old marker image(red) new marker(blue)
i mean have performed manual test. came mapactivity shows red pin. fine. if perform auto test (from mapactivity) should update red pin blue pin doesnot update if click on pin show both pins 1 one. if switch other activity , come map activity shows blue.
maybe before adding marks should clear old ones? map.clear();
Comments
Post a Comment