I made an Android app that gets the location in longitude and latitude with the click of a button.
First, I get the last known location, which is inaccurate for argumentation, when I first download the application / enable gps.
What I would like to know is to wait until it is accurate, for example, on Google maps, when you receive the message โWaiting for location.โ
If you see any way that can be improved, it will also be useful.
Code for reference:
public class Clue extends Activity { public static double latitude; public static double longitude; Criteria criteria; LocationManager lm; LocationListener ll; Location location; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.questions); criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); ll = new MyLocationListener(); lm.requestLocationUpdates(lm.getBestProvider(criteria, true), 0, 0, ll); } private boolean weAreThere() { location = getLocation(); longitude = location.getLongitude(); latitude = location.getLatitude(); return inCorrectPlace(param); } private Location getLocation() { lm.requestLocationUpdates(lm.getBestProvider(criteria, true), 0, 0, ll); return lm.getLastKnownLocation(lm.getBestProvider(criteria, true)); } } public class MyLocationListener implements LocationListener { public void onLocationChanged(Location loc) { Clue.longitude = loc.getLongitude(); Clue.latitude = loc.getLatitude(); } }
Thanks for reading, all answers will be appreciated.
Ben
source share