i am trying to get the current location in android for that i have used the android and following is my code
locationManager = (LocationManager) getSystemService(this.context.LOCATION_SERVICE);
Criteria crta = new Criteria();
crta.setAccuracy(Criteria.ACCURACY_FINE);
crta.setAltitudeRequired(true);
crta.setBearingRequired(true);
crta.setCostAllowed(true);
crta.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(crta, true);
Log.d("","provider : "+provider);
provider = LocationManager.GPS_PROVIDER;
Log.d("","provider : "+provider);
locationManager.requestLocationUpdates(provider, 0, 0, (LocationListener) this);
Location location = locationManager.getLastKnownLocation(provider);
//isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (location != null)
{
double latitude = location.getLatitude();
double longitude = location.getLongitude();
txtLat.setText("Latitude"+latitude+"longitude"+longitude);
Log.d("not null", "location is not null");
}
else
{
Toast.makeText(getApplicationContext(), "locatyion is null", Toast.LENGTH_LONG).show();
Log.d("null","location is null");
}
The problem is that it doesnt gives me my current location when i open the application the location is null. Can anyone help ? i have seen many examples and tried others too but didnt work. I have added
if (!isNetworkEnabled)
{
Toast.makeText(getApplicationContext(), "No network provider the gps wont work", Toast.LENGTH_LONG).show();
// no network provider is enabled
}
else{
Toast.makeText(getApplicationContext(), "Network is available", Toast.LENGTH_LONG).show();
}
and its says gps is available and network is not available