Wait For Current Location - Gps - Android Dev
I have to wait to get the current location, just like when you are in Google maps and it requests for your location. If it can't get any location inform the error and do nothing.
Solution 1:
Read this from yesterday: GPS Android - get positioning only once
But here is how you can get the current location:
publicclassExampleextendsActivityimplementsLocationListener {
LocationManager mLocationManager;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
// Do something with the recent location fix // if it is less than two minutes old,// otherwise wait for the update below
}
}
publicvoidonLocationChanged(Location location) {
if (location != null) {
Log.v("Location Changed", location.getLatitude() + " and " + location.getLongitude());
// You need to call this whenever you are done:// mLocationManager.removeUpdates(this);
}
}
// Required functions publicvoidonProviderDisabled(String arg0) {}
publicvoidonProviderEnabled(String arg0) {}
publicvoidonStatusChanged(String arg0, int arg1, Bundle arg2) {}
}
Post a Comment for "Wait For Current Location - Gps - Android Dev"