java - How to display lat/long markers on google map v2 android -
i'm working on android app web services, using external database. app consists of several tasks , each task assigned each camera id. each camera have specific long/lat stored in database. right when select specific task, there guide button showing user current location marker in google map. want display long/lat of task marker on google map, there 2 marker. need android coding. appreciated!
googlemapactivity.java
public class googlemapactivity extends activity implements locationlistener { googlemap map; private string cameraid; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_google_map); locationmanager lm = (locationmanager) getsystemservice(context.location_service); lm.requestlocationupdates(locationmanager.network_provider, 0, 0, this); map = ((mapfragment) getfragmentmanager().findfragmentbyid(r.id.map)) .getmap(); } @override public void onlocationchanged(location location) { map.clear(); markeroptions mp = new markeroptions(); mp.position(new latlng(location.getlatitude(), location.getlongitude())); mp.title("my current location"); map.addmarker(mp); map.animatecamera(cameraupdatefactory.newlatlngzoom( new latlng(location.getlatitude(), location.getlongitude()), 16)); } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub } @override public void onproviderenabled(string provider) { // todo auto-generated method stub } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } }
accepttask.java
public class accepttask extends sherlockactivity { private static string sendingurl, url, imageurl, datetimep, notificationid, cameraid; private string check = null; // json node names private static final string tag_get = "jobviewresult", tag_imageurl = "imageurl", tag_message = "vmessage", tag_geta = "assignresult", tag_status = "assignstatus"; private progressdialog pdialog; public static final int progress_bar_type = 0; private textview tvcamera, tvdate; private imageview my_image; // contacts jsonobject jsonobject api = null; private long userinteractiontime = 0; asynctask<void, void, void> mregistertask; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_accept_task); tvcamera = (textview) findviewbyid(r.id.tv_cameraid); tvdate = (textview) findviewbyid(r.id.tv_dater); my_image = (imageview) findviewbyid(r.id.img_task); //set icon clickable getsupportactionbar().sethomebuttonenabled(true); //add < arrow on icon getsupportactionbar().setdisplayhomeasupenabled(true); file file = new file("/sdcard/locationsystem/pendingtask_temp.jpg" ); if (file.exists()) { file.delete(); } if(isnetworkconnected()){ try{ // getting intent data intent intent = getintent(); /** values previous intent */ datetimep = intent.getstringextra("tag_image_time_date_p"); cameraid = intent.getstringextra("tag_camera_id_p"); notificationid = intent.getstringextra("tag_notification_id"); /** display cameraid , date time */ tvcamera.settext(cameraid); tvdate.settext(datetimep); url = "url"; // creating json parser instance jsonparser jparser = new jsonparser(); // getting json string url jsonobject json = jparser.getjsonfromurl(url); try { api = json.getjsonobject(tag_get); check = api.getstring(tag_message); // storing each json item in variable imageurl = api.getstring(tag_imageurl); } catch (jsonexception e) { e.printstacktrace(); } if(check.equals("job available")){ new downloadimagetask().execute(imageurl); }else{ alertdialog.builder builder = new alertdialog.builder(new contextthemewrapper(this, r.style.alertdialogtheme)); builder.setcancelable(false); builder.settitle("job unavailable"); builder.setmessage(check); // setting icon dialog builder.seticon(r.drawable.ic_action_error); builder.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { intent loadintent = new intent(); loadintent.putextra("startupdate_plist", "start"); setresult(activity.result_ok, loadintent); finish(); } }); builder.show(); } } catch(exception e) { if(e != null) { showservererrordialog(); } } }else{shownetworkerrordialog();} } private void showservererrordialog(){ alertdialog.builder builder = new alertdialog.builder(new contextthemewrapper(this, r.style.alertdialogtheme)); builder.setcancelable(false); builder.settitle("server error"); builder.setmessage("server unable connect. please check network connectivity."); // setting icon dialog builder.seticon(r.drawable.ic_action_error); builder.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { finish(); } }); builder.show(); } private void shownetworkerrordialog(){ alertdialog.builder builder = new alertdialog.builder(new contextthemewrapper(this, r.style.alertdialogtheme)); builder.settitle("no network connection"); builder.setmessage("please turn on mobile 3g or connect network in order use application."); // setting icon dialog builder.seticon(r.drawable.ic_action_error); builder.setpositivebutton("close", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { accepttask.this.finish(); } }); builder.setnegativebutton("setting", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { //calling setting menu startactivityforresult(new intent(android.provider.settings.action_settings), 0); } }); builder.show(); } private boolean isnetworkconnected() { connectivitymanager cm = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo ni = cm.getactivenetworkinfo(); if (ni == null) { return false; } else{ return true; } } @override public boolean onoptionsitemselected(menuitem item) { // handle presses on action bar items switch (item.getitemid()) { case android.r.id.home: if(my_image.getdrawable() != null){ file file = new file("/sdcard/locationsystem/pendingtask_temp.jpg" ); if (file.exists()) { file.delete(); } } intent loadintent = new intent(); loadintent.putextra("startupdate_plist", "start"); setresult(activity.result_ok, loadintent); finish(); break; default: return super.onoptionsitemselected(item); } return false; } @override public void onuserinteraction() { userinteractiontime = system.currenttimemillis(); super.onuserinteraction(); log.i("appname","interaction"); } @override public void onuserleavehint() { long uidelta = (system.currenttimemillis() - userinteractiontime); super.onuserleavehint(); if (uidelta < 100){ logout(); } } @override public void onresume() { super.onresume(); if(isnetworkconnected()){ try{ sharedpreferences checklogin1 = getapplicationcontext().getsharedpreferences( "checklogin", 0); string staffid = checklogin1.getstring("staffid", ""); string staffpassword = checklogin1.getstring("password", ""); defaulthttpclient httpclient = new defaulthttpclient(); //get unique id sharedpreferences pref = getapplicationcontext().getsharedpreferences( "checklogin", 0); string checkid = pref.getstring("loginrid", ""); httpget request = new httpget(url); request.setheader("accept", "application/json"); request.setheader("content-type", "application/json"); httpresponse response = httpclient.execute(request); httpentity responseentity = response.getentity(); // read response data buffer char[] buffer = new char[(int) responseentity .getcontentlength()]; inputstream stream = responseentity.getcontent(); inputstreamreader reader = new inputstreamreader(stream); reader.read(buffer); stream.close(); jsonobject svr = new jsonobject(new string(buffer)); jsonobject obj = svr.getjsonobject("logintestresult"); string validation = obj.getstring("valid"); string position = obj.getstring("position"); string companyid = obj.getstring("companyid"); string messagestatus = obj.getstring("message"); if(validation.equals("1")){ checknotnull(server_url, "server_url"); checknotnull(sender_id, "sender_id"); // make sure device has proper dependencies. gcmregistrar.checkdevice(this); // make sure manifest set - comment out line // while developing app, uncomment when it's ready. gcmregistrar.checkmanifest(this); //mdisplay = (textview) findviewbyid(r.id.display); //gcmidtxt =(textview) findviewbyid(r.id.gcmid); registerreceiver(mhandlemessagereceiver, new intentfilter(display_message_action)); final string regid = gcmregistrar.getregistrationid(this); system.out.print(regid); if (regid.equals("")) { // automatically registers application on startup. gcmregistrar.register(this, sender_id); } else { // device registered on gcm, check server. // if (gcmregistrar.isregisteredonserver(this)) { // // skips registration. // //mdisplay.append(getstring(r.string.already_registered) + "\n"); // //gcmidtxt.settext(regid); // // } else { // try register again, not in ui thread. // it's necessary cancel thread ondestroy(), // hence use of asynctask instead of raw thread. final context context = this; mregistertask = new asynctask<void, void, void>() { @override protected void doinbackground(void... params) { boolean registered = serverutilities.register(context, regid); // @ point attempts register app // server failed, need unregister device // gcm - app try register again when // restarted. note gcm send // unregistered callback upon completion, // gcmintentservice.onunregistered() ignore it. if (!registered) { gcmregistrar.unregister(context); } return null; } @override protected void onpostexecute(void result) { mregistertask = null; } }; mregistertask.execute(null, null, null); // } } }else{ alertdialog.builder builder = new alertdialog.builder(new contextthemewrapper(this, r.style.alertdialogtheme)); builder.setcancelable(false); builder.settitle("error"); builder.setmessage(messagestatus + " please sign in again."); // setting icon dialog builder.seticon(android.r.drawable.ic_dialog_alert); builder.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { sharedpreferences pref2 = getapplicationcontext().getsharedpreferences("checklogin", 0); final sharedpreferences.editor editor1 = pref2.edit(); editor1.putint("login", 0); editor1.commit(); intent intent = new intent(accepttask.this, loginpage.class); intent.addflags(intent.flag_activity_clear_top); startactivity(intent); finish(); } }); builder.show(); } } catch(exception e) { if(e != null) { showservererrordialog(); } } }else{ shownetworkerrordialog(); } //runacceptedtask(); } private final broadcastreceiver mhandlemessagereceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { string newmessage = intent.getextras().getstring(extra_message); //mdisplay.append(newmessage + "\n"); } }; private void checknotnull(object reference, string name) { if (reference == null) { throw new nullpointerexception( getstring(r.string.error_config, name)); } } private void logout(){ if(isnetworkconnected()){ try{ strictmode.threadpolicy policy = new strictmode.threadpolicy.builder() .permitall().build(); strictmode.setthreadpolicy(policy); defaulthttpclient httpclient = new defaulthttpclient(); //get unique id sharedpreferences pref = getapplicationcontext().getsharedpreferences( "checklogin", 0); string checkid = pref.getstring("loginrid", ""); string staffid = pref.getstring("staffid", ""); httpget request = new httpget(url); request.setheader("accept", "application/json"); request.setheader("content-type", "application/json"); httpresponse response = httpclient.execute(request); httpentity responseentity = response.getentity(); // read response data buffer char[] buffer = new char[(int) responseentity .getcontentlength()]; inputstream stream = responseentity.getcontent(); inputstreamreader reader = new inputstreamreader(stream); reader.read(buffer); stream.close(); jsonobject svr = new jsonobject(new string(buffer)); jsonobject obj = svr.getjsonobject("logoutresult"); string messagestatus = obj.getstring("usermessage"); if(messagestatus.equals("updated successfully!!")){ }else{ alertdialog.builder builder = new alertdialog.builder(new contextthemewrapper(this, r.style.alertdialogtheme)); builder.setcancelable(false); builder.settitle("error"); builder.setmessage(messagestatus); // setting icon dialog builder.seticon(r.drawable.ic_action_error); builder.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { finish(); } }); builder.show(); } } catch (exception e) { if(e != null) { showservererrordialog(); } } } else{ shownetworkerrordialog(); } } //accept task button public void accepttask(view v) { file file = new file("/sdcard/locationsystem/pendingtask_temp.jpg" ); if (file.exists()) { file.delete(); } alertdialog.builder builder = new alertdialog.builder(accepttask.this); builder.settitle("confirm accept"); builder.setmessage("are sure accept job?"); builder.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { if(isnetworkconnected()){ try{ // do action here sharedpreferences pref = getapplicationcontext().getsharedpreferences( "checklogin", 0); string staffid = pref.getstring("staffid", ""); //sendingurl = ""+staffid; sendingurl = "url"; // creating json parser instance jsonparser jparser = new jsonparser(); // getting json string url jsonobject json = jparser.getjsonfromurl(sendingurl); try { api = json.getjsonobject(tag_geta); check = api.getstring(tag_status); } catch (jsonexception e) { if(e != null) { showservererrordialog();} } if(check.equals("job available")){ defaulthttpclient httpclient = new defaulthttpclient(); httpget httpget = new httpget(sendingurl); try { httpresponse response = httpclient.execute(httpget); toast.maketext(getapplicationcontext(), "you accepted job.", toast.length_short).show(); intent loadintent = new intent(); loadintent.putextra("startupdate_plist", "start"); setresult(activity.result_ok, loadintent); finish(); } catch (ioexception e) { if(e != null) { showservererrordialog();} } }else{ alertdialog.builder builder = new alertdialog.builder(new contextthemewrapper(accepttask.this, r.style.alertdialogtheme)); builder.setcancelable(false); builder.settitle("job unavailable"); builder.setmessage(check); // setting icon dialog builder.seticon(r.drawable.ic_action_error); builder.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { intent loadintent = new intent(); loadintent.putextra("startupdate_plist", "start"); setresult(activity.result_ok, loadintent); finish(); } }); builder.show(); } } catch(exception e) { if(e != null) { showservererrordialog(); } } }else{ shownetworkerrordialog(); } } }); builder.setnegativebutton("no", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); alertdialog alert = builder.create(); alert.show(); } private class downloadimagetask extends asynctask<string, string, string> { @override protected void onpreexecute() { super.onpreexecute(); showdialog(progress_bar_type); } protected string doinbackground(string... urls) { int count; try { url url = new url(urls[0]); urlconnection connection = url.openconnection(); connection.connect(); int lenghtoffile = connection.getcontentlength(); inputstream in = new bufferedinputstream(url.openstream(), 8192); byte data[] = new byte[1024]; // output stream write file outputstream output = new fileoutputstream("/sdcard/locationsystem/pendingtask_temp.jpg"); long total = 0; while((count = in.read(data)) != -1){ total += count; publishprogress(""+(int)((total*100)/lenghtoffile)); file file = new file(getexternalcachedir(), "pendingtask_temp.jpg" ); if (file.exists()) { file.delete(); }else{ output.write(data, 0, count); } } output.flush(); output.close(); in.close(); } catch (exception e) { log.e("error", e.getmessage()); if(e != null) { showservererrordialog(); } } return null; } /** * updating progress bar * */ protected void onprogressupdate(string... progress) { // setting progress percentage pdialog.setprogress(integer.parseint(progress[0])); } protected void onpostexecute(string file_url) { // dismiss dialog after file downloaded dismissdialog(progress_bar_type); string imagepath = environment.getexternalstoragedirectory().tostring() + "/locationsystem/pendingtask_temp.jpg"; // setting downloaded image view my_image.setimagedrawable(drawable.createfrompath(imagepath)); file file = new file("/sdcard/locationsystem/pendingtask_temp.jpg" ); if (file.exists()) { file.delete(); } if (my_image.getdrawable() == null){ toast.maketext(accepttask.this, "please select again!",toast.length_long).show(); finish(); } } } @override protected dialog oncreatedialog(int id) { switch (id) { case progress_bar_type: // set 0 pdialog = new progressdialog(this); pdialog.setmessage("downloading file. please wait..."); pdialog.setindeterminate(false); pdialog.setmax(100); pdialog.setprogressstyle(progressdialog.style_horizontal); //pdialog.setbutton("cancel", (dialoginterface.onclicklistener) null); pdialog.setbutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { accepttask.this.finish(); } }); pdialog.setcancelable(false); pdialog.show(); return pdialog; default: return null; } } @override public void onbackpressed() { if(my_image.getdrawable() != null){ file file = new file("/sdcard/locationsystem/pendingtask_temp.jpg" ); if (file.exists()) { file.delete(); } } intent loadintent = new intent(); loadintent.putextra("startupdate_plist", "start"); setresult(activity.result_ok, loadintent); finish(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. menuinflater inflater = getsupportmenuinflater(); inflater.inflate(r.menu.accept_task, menu); return true; } }
you can asynck task db values , call task in onlocationchange.
Comments
Post a Comment