google play services - get current location of android device -



google play services - get current location of android device -

i have taken code google "googleplacelocation" project. trying should give me current nearest location of mobile, using longitude , latitude comparing .

right giving me list view of close places through when click on place name shows me address, name , longitude , latitude. i'm trying closest nearby location. has other files such googleplaces , place.java , on. work done here.

here code

mainactivity.java

public class mainactivity extends activity { // flag net connection status boolean isinternetpresent = false; // connection detector class connectiondetector cd; // alert dialog manager alertdialogmanager alert = new alertdialogmanager(); // google places googleplaces googleplaces; // places list placeslist nearplaces; // gps location gpstracker gps; // button button btnshowonmap; // progress dialog progressdialog pdialog; // places listview listview lv; // listitems info arraylist<hashmap<string, string>> placeslistitems = new arraylist<hashmap<string,string>>(); // key strings public static string key_reference = "reference"; // id of place public static string key_name = "name"; // name of place public static string key_vicinity = "vicinity"; // place area name @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); cd = new connectiondetector(getapplicationcontext()); // check if net nowadays isinternetpresent = cd.isconnectingtointernet(); if (!isinternetpresent) { // net connection not nowadays // alert.showalertdialog(mainactivity.this, "internet connection error", // "please connect working net connection", false); // stop executing code homecoming wifimanager wifimanager = (wifimanager)this.getsystemservice(context.wifi_service); wifimanager.setwifienabled(true); return; } // creating gps class object gps = new gpstracker(this); // check if gps location can if (gps.cangetlocation()) { log.d("your location", "latitude:" + gps.getlatitude() + ", longitude: " + gps.getlongitude()); } else { // can't user's current location alert.showalertdialog(mainactivity.this, "gps status", "couldn't location information. please enable gps", false); // stop executing code homecoming return; } // getting listview lv = (listview) findviewbyid(r.id.list); // button show on map btnshowonmap = (button) findviewbyid(r.id.btn_show_map); // calling background async task load google places // after getting places google info shown in listview new loadplaces().execute(); /** button click event shown on map */ btnshowonmap.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { intent = new intent(getapplicationcontext(), placesmapactivity.class); // sending user current geo location i.putextra("user_latitude", double.tostring(gps.getlatitude())); i.putextra("user_longitude", double.tostring(gps.getlongitude())); // passing near places map activity i.putextra("near_places", nearplaces); // staring activity startactivity(i); } }); /** * listitem click event * on selecting listitem singleplaceactivity launched * */ lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // getting values selected listitem string reference = ((textview) view.findviewbyid(r.id.reference)).gettext().tostring(); // starting new intent intent in = new intent(getapplicationcontext(), singleplaceactivity.class); // sending place refrence id single place activity // place refrence id used "place total details" in.putextra(key_reference, reference); startactivity(in); } }); } /** * background async task load google places * */ class loadplaces extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(mainactivity.this); pdialog.setmessage(html.fromhtml("<b>search</b><br/>loading places...")); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } /** * getting places json * */ protected string doinbackground(string... args) { // creating places class object googleplaces = new googleplaces(); seek { // separeate place types pipe symbol "|" // if want types places create null // check list of types supported google // string types = "cafe|restaurant"; // listing places cafes, restaurants // radius in meters - increment value if don't find places double radius = 1000; // 1000 meters // nearest places nearplaces = googleplaces.search(gps.getlatitude(), gps.getlongitude(), radius, types); } grab (exception e) { e.printstacktrace(); } homecoming null; } /** * after completing background task dismiss progress dialog * , show info in ui * utilize runonuithread(new runnable()) update ui background * thread, otherwise error * **/ protected void onpostexecute(string file_url) { // dismiss dialog after getting products pdialog.dismiss(); // updating ui background thread runonuithread(new runnable() { public void run() { /** * updating parsed places listview * */ // json response status string status = nearplaces.status; // check possible status if(status.equals("ok")){ // got places details if (nearplaces.results != null) { // loop through each place (place p : nearplaces.results) { hashmap<string, string> map = new hashmap<string, string>(); // place reference won't display in listview - hidden // place reference used "place total details" map.put(key_reference, p.reference); // place name map.put(key_name, p.name);// lati using list // adding hashmap arraylist placeslistitems.add(map); } // list adapter listadapter adapter = new simpleadapter(mainactivity.this, placeslistitems, r.layout.list_item, new string[] { key_reference, key_name}, new int[] { r.id.reference, r.id.name }); // adding info listview lv.setadapter(adapter); } } else if(status.equals("zero_results")){ // 0 results found alert.showalertdialog(mainactivity.this, "near places", "sorry no places found. seek alter types of places", false); } else if(status.equals("unknown_error")) { alert.showalertdialog(mainactivity.this, "places error", "sorry unknown error occured.", false); } else if(status.equals("over_query_limit")) { alert.showalertdialog(mainactivity.this, "places error", "sorry query limit google places reached", false); } else if(status.equals("request_denied")) { alert.showalertdialog(mainactivity.this, "places error", "sorry error occured. request denied", false); } else if(status.equals("invalid_request")) { alert.showalertdialog(mainactivity.this, "places error", "sorry error occured. invalid request", false); } else { alert.showalertdialog(mainactivity.this, "places error", "sorry error occured.", false); } } }); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.activity_main, menu); homecoming true; } }

android google-play-services

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -