tags:

views:

404

answers:

1

hii i have a problem.I want to find latitude and longitude of my current location in android application.Its not on location changed . I just need when i am stable with my phone

pliz help..thx in advance.

My code is:

LocationManager locationManager; locationManager = (LocationManager)getSystemService (Context.LOCATION_SERVICE); Location location = locationManager.getLastKnownLocation (LocationManager.GPS_PROVIDER);

locationManager.requestLocationUpdates(                
         LocationManager.GPS_PROVIDER,0,0,(LocationListener) this);
updateWithNewLocation(location);
}

private void updateWithNewLocation(Location location) {
TextView myLocationText = (TextView)findViewById(R.id.myLocationText);

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);
}

after running it on emulator i always find "no location found".Why this is so? Can anyone answer it?

Please rply.. Thanks

+1  A: 

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.

CommonsWare