Retrieve GPS location using android -
i'm trying gps location of current location using simple android program. used onlocationchanged()
method this. returning coordinates 0.0. users permission part correct. code of main one.
public class mainactivity extends activity { gpstracker gps; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button btn = (button) findviewbyid(r.id.button1); final textview tv = (textview) findviewbyid(r.id.textview1); btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub gps = new gpstracker(mainactivity.this); double lat = gps.latitude; string mlat = string.valueof(lat); tv.settext(mlat); } }); } }
this gps location class im using...
public class gpstracker extends service implements locationlistener{ private final context mcontext; boolean isgpsenabled = false; boolean cangetlocation = false; location location; double latitude; double longtitude; protected locationmanager locationmanager; public gpstracker(context context){ this.mcontext = context; getlocation(); } public location getlocation(){ locationmanager = (locationmanager) mcontext.getsystemservice(location_service); locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, this); return null; } protected void onstart() { } protected void onpause(){ locationmanager.removeupdates(this); } @override public void onlocationchanged(location location) { // todo auto-generated method stub latitude = location.getlatitude(); longtitude = location.getlongitude(); } @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 } @override public ibinder onbind(intent arg0) { // todo auto-generated method stub return null; } }
you can find location either gps_provider or network_provider
overview of location services in android
here 1 example try find location using gps , if gps not available try use network find location
gpstracker.java
public class gpstracker extends service implements locationlistener { private final context mcontext; // flag gps status boolean isgpsenabled = false; // flag network status boolean isnetworkenabled = false; // flag gps status boolean cangetlocation = false; location location; // location double latitude; // latitude double longitude; // longitude // minimum distance change updates in meters private static final long min_distance_change_for_updates = 10; // 10 meters // minimum time between updates in milliseconds private static final long min_time_bw_updates = 1000 * 60 * 1; // 1 minute // declaring location manager protected locationmanager locationmanager; public gpstracker(context context) { this.mcontext = context; getlocation(); } public location getlocation() { try { locationmanager = (locationmanager) mcontext .getsystemservice(location_service); // getting gps status isgpsenabled = locationmanager .isproviderenabled(locationmanager.gps_provider); // getting network status isnetworkenabled = locationmanager .isproviderenabled(locationmanager.network_provider); if (!isgpsenabled && !isnetworkenabled) { // no network provider enabled } else { this.cangetlocation = true; if (isnetworkenabled) { locationmanager.requestlocationupdates( locationmanager.network_provider, min_time_bw_updates, min_distance_change_for_updates, this); log.d("network", "network"); if (locationmanager != null) { location = locationmanager .getlastknownlocation(locationmanager.network_provider); if (location != null) { latitude = location.getlatitude(); longitude = location.getlongitude(); } } } // if gps enabled lat/long using gps services if (isgpsenabled) { if (location == null) { locationmanager.requestlocationupdates( locationmanager.gps_provider, min_time_bw_updates, min_distance_change_for_updates, this); log.d("gps enabled", "gps enabled"); if (locationmanager != null) { location = locationmanager .getlastknownlocation(locationmanager.gps_provider); if (location != null) { latitude = location.getlatitude(); longitude = location.getlongitude(); } } } } } } catch (exception e) { e.printstacktrace(); } return location; } /** * stop using gps listener * calling function stop using gps in app * */ public void stopusinggps(){ if(locationmanager != null){ locationmanager.removeupdates(gpstracker.this); } } /** * function latitude * */ public double getlatitude(){ if(location != null){ latitude = location.getlatitude(); } // return latitude return latitude; } /** * function longitude * */ public double getlongitude(){ if(location != null){ longitude = location.getlongitude(); } // return longitude return longitude; } /** * function check gps/wifi enabled * @return boolean * */ public boolean cangetlocation() { return this.cangetlocation; } /** * function show settings alert dialog * on pressing settings button lauch settings options * */ public void showsettingsalert(){ alertdialog.builder alertdialog = new alertdialog.builder(mcontext); // setting dialog title alertdialog.settitle("gps settings"); // setting dialog message alertdialog.setmessage("gps not enabled. want go settings menu?"); // on pressing settings button alertdialog.setpositivebutton("settings", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int which) { intent intent = new intent(settings.action_location_source_settings); mcontext.startactivity(intent); } }); // on pressing cancel button alertdialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); // showing alert message alertdialog.show(); } @override public void onlocationchanged(location location) { } @override public void onproviderdisabled(string provider) { } @override public void onproviderenabled(string provider) { } @override public void onstatuschanged(string provider, int status, bundle extras) { } @override public ibinder onbind(intent arg0) { return null; } }
activity -androidgpstrackingactivity.java
public class androidgpstrackingactivity extends activity { button btnshowlocation; // gpstracker class gpstracker gps; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); btnshowlocation = (button) findviewbyid(r.id.btnshowlocation); // show location button click event btnshowlocation.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { // create class object gps = new gpstracker(androidgpstrackingactivity.this); // check if gps enabled if(gps.cangetlocation()){ double latitude = gps.getlatitude(); double longitude = gps.getlongitude(); // \n new line toast.maketext(getapplicationcontext(), "your location - \nlat: " + latitude + "\nlong: " + longitude, toast.length_long).show(); }else{ // can't location // gps or network not enabled // ask user enable gps/network in settings gps.showsettingsalert(); } } }); } }
layout- main.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <button android:id="@+id/btnshowlocation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="show location" android:layout_centervertical="true" android:layout_centerhorizontal="true"/> </relativelayout> <uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="android.permission.internet" />
Comments
Post a Comment