i m working on app, which must get latitude and longitude. in my case requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener) is not in option. why? because i must get location just when user do something that location is needed (let s say he hits button). and i need location exactly on the time, when button is hit. in requestLocationUpdates, we can set minTime-if i set this let s say on 30000 the location at the "button hit time" won t be as good as i want. of the same reason minDistance is also not as good as i want. if i understant function requestLocationUpdates correct-when minTime and minDistance are set to 0, location is updating all the time. please correct me if i m wrong. app is for company that i m working at and that app will be used through the day and night. so if app will check for location updates all the time, battery would be often empty. that s why i need location just at the time, that button is hit. users are not having access to internet and gps. so i must use NETWORK_PROVIDER
I also have problems with getting location after reboot device. it doesnt work until i run google maps. users wont have avalible this either, so what function do google maps on android use to get location after reboot?
at the moment i use this code:
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener); mlocManager.removeUpdates(mlocListener); public class MyLocationListener implements LocationListener{ @Override public void onLocationChanged(Location loc){ latitude=loc.getLatitude(); longitude=loc.getLongitude(); Text = "My current location is: " + "Latitud = " + loc.getLatitude() + " Longitud = " + loc.getLongitude(); Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show(); } @Override public void onProviderDisabled(String provider){} @Override public void onProviderEnabled(String provider){} @Override public void onStatusChanged(String provider, int status, Bundle extras){} }
http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/ http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/
but this code is not working right. if i press "get location button" the Toast is shown and location is correct. but then if i press button several times, it does nothing. -but after 5 minutes Toast is shown like hundred times... why is that?