find current location latitude and longitude in Android [duplicate]
public class GPSmap extends Activity {
GeoPoint geoPoint;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager locManager;
setContentView(R.layout.main);
locManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,
500.0f, locationListener);
Location location = locManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}
}
private void updateWithNewLocation(Location location) {
TextView myLocationText = (TextView) findViewById(R.id.text);
String latLongString = "";
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
} else {
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" + latLongString);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider,int status,Bundle extras){}
};
}
If you will use this code, you will get answer.
The GPS radio is not kept on all of the time, as it seriously drains the battery. At the point when you are calling getLastKnownLocation()
, the radio is probably off.
You need to do your requestLocationUpdates()
first, then wait for a fix to be supplied to your LocationListener
. Any time after that (until you removeUpdates()
), getLastKnownLocation()
should return a non-null
value.
I have search lots of tutorial but found this best one:
Using Criteria
and BestProvider
/** PROCESS for Get Longitude and Latitude **/
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Log.d("msg", "GPS:"+isGPSEnabled);
// check if GPS enabled
if(isGPSEnabled){
/*
longitude = 70.80079728674089;
latitude = 22.29090332494221;
*/
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
//new LoadPlaces().execute();
//Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)
{
longitude = location.getLongitude();
latitude = location.getLatitude();
Log.d("msg", "first lat long : "+latitude +" "+ longitude);
//new LoadPlaces().execute();
}else
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
@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
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
longitude = location.getLongitude();
latitude = location.getLatitude();
Log.d("msg", "changed lat long : "+latitude +" "+ longitude);
}
});
}
}
else
{
showAlertforGPS();
}
The emulator by default, may not have any location associated. So it is not giving any.
To send a location, go to DDMS perspective in Eclipse, and enter the latitude and longitude and click Send. Then you will be able to see that in your application.
Let me know if you still have any issues.