java - Get current user location -
i'm trying current user location on devices works, on im stuck waiting location.
here code:
private final locationlistener mlocationlistener = new locationlistener() { @override public void onlocationchanged(final location location) { // todo log.v("--", "get address 121"); main.this.location = location; getaddress(); locationmanager.removeupdates(mlocationlistener); } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } @override public void onproviderenabled(string provider) { // todo auto-generated method stub log.v("--", "provider enabled"); } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub log.v("--", "provider disabled"); } }; public void putextra() { intent intent = new intent(this, settings.class); intent.putextra("timesobj", times); startactivity(intent); } /** * current/last known location , display city, country name , update * calculations * */ public void getaddress() { log.v("--", "get address 1"); boolean isgpsproviderenabled = locationmanager .isproviderenabled(locationmanager.gps_provider); boolean network_enabled = locationmanager .isproviderenabled(locationmanager.network_provider); log.v("--", "get address 31 " + isgpsproviderenabled + " gps - " + isconnectedtonetwork()); if (isgpsproviderenabled || network_enabled) { log.v("--", "get address 2"); criteria c = new criteria(); log.v("--", "provider " + locationmanager.getbestprovider(c, true)); location = locationmanager.getlastknownlocation(locationmanager .getbestprovider(c, false)); if (location == null) { log.v("--", "get address 6"); locationmanager.requestlocationupdates( locationmanager.getbestprovider(c, false), 1000, 100, mlocationlistener); } else { log.v("--", "get address 3"); if (isconnectedtonetwork()) { new asynctask<void, void, void>() { protected void doinbackground(void... params) { try { com.myapp.utils.geocoder geocoder = new com.myapp.utils.geocoder( main.this); geocodermodel geocodermodel = geocoder .getfromlocation( location.getlatitude(), location.getlongitude(), 5); city = geocodermodel.getcity(); country = geocodermodel.getcountry(); prefs.edit().putstring(constants.city, city) .apply(); log.v("--", "get address 4"); } catch (ioexception e) { log.v("--", "get address 11"); e.printstacktrace(); } catch (limitexceededexception e) { log.v("--", "get address 12"); e.printstacktrace(); } return null; }; protected void onpostexecute(void result) { prefs.edit().putstring(constants.country, country) .apply(); prefs.edit().putstring(constants.city, city) .apply(); populatelist(location); }; }.execute(); } else { city = prefs.getstring(constants.city, getstring(r.string.app_name)); log.v("--", "get address 33 " + location.getlatitude()); populatelist(location); } } } else { log.v("--", "get address 5"); startgpsenabledialog(); } }
and here log have:
v/-- ( 5498): address 1 v/-- ( 5498): address 31 true gps - true v/-- ( 5498): address 2 v/-- ( 5498): provider gp v/-- ( 5498): address 6
can me can wrong in code, , tell me how can user location correctly?
thanks!
try change line
locationmanager.getbestprovider(c, false)
to
locationmanager.getbestprovider(c, true)
as stated here, "enabledonly - if true provider enabled returned". might provider not enabled.
1 more thing, seem using criteria, not using looking @ code looks don't it. suggest take @ it. found code sample here might you.
private void startlocationstuff(){ locmanager=(locationmanager)getsystemservice(context.location_service); loclistener=new mylocationlistener(); final criteria criteria=new criteria(); criteria.setaccuracy(criteria.accuracy_fine); criteria.setaltituderequired(false); criteria.setbearingrequired(false); criteria.setcostallowed(true); criteria.setpowerrequirement(criteria.power_low); bestprovider=locmanager.getbestprovider(criteria,true); }
you might want consider using location manager without criteria this.
Comments
Post a Comment