Get String from Location Android -
i found great function gives me possibility location quickly. after want display (longitude , latitude) doest work still 0.0 / 0.0.
here's code
what should if want display latitude , longitude i'm interested in function getlastknownlocation because want display data without using gps or internet
public class mainactivity extends activity implements onclicklistener
{
private static final long min_time_bw_updates = 0; private static final float min_distance_change_for_updates = 0; public edittext lokalizacja; public button pobierz; private static context context; locationmanager locationmanager; // 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 public static context getcontext() { return context; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); lokalizacja = (edittext) findviewbyid(r.id.edittext1); pobierz = (button) findviewbyid(r.id.button1); pobierz.setonclicklistener(this); } @override public void onclick(view v) { getlocation(); } public location getlocation() { try { locationmanager = (locationmanager) context.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, (locationlistener) this); log.d("network", "network enabled"); 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, (locationlistener) this); log.d("gps", "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(); } string napis = string.valueof(latitude+ "\n" + longitude); lokalizacja.settext(napis); return location; }
}
as flow never enter else part due following condition
if (!isgpsenabled && !isnetworkenabled) { // no network provider enabled } else {
you need have condition this
if (!isgpsenabled || !isnetworkenabled) { // no network provider enabled } else {
or first check gps , after network
Comments
Post a Comment