1

I want the location of user and that too just once after that user navigates on his own

locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
if (location != null) {
   System.out.println(location.toString());
   lat=location.getLatitude();
   lng=location.getLongitude();
}     
p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

However i am not able to get the location of the user is it because of slow internet connection from the mobile ??

Janusz
  • 187,060
  • 113
  • 301
  • 369
A.K
  • 29
  • 5
  • possible duplicate of [find current location latitude and longitude](http://stackoverflow.com/questions/2250597/find-current-location-latitude-and-longitude) – CommonsWare May 29 '11 at 11:03
  • 1
    Also possible duplicate of http://stackoverflow.com/questions/3682998/android-how-often-does-getlastknownlocationlocationmanager-network-provider-re and http://stackoverflow.com/questions/1916568/getlastknownlocation-getting-null-on-sdk and http://stackoverflow.com/questions/4474470/location-is-null-is-it-the-same-behavior-in-real-device and http://stackoverflow.com/questions/5674414/location-returned-is-null-when-provider-is-gps and many others – CommonsWare May 29 '11 at 11:04
  • i had tried lots of options and was yet not able to solve so due to restlessness had posted it anyways solved it . – A.K Jun 01 '11 at 04:53

2 Answers2

0

It's not enough to just get a locationManager and call getLastKnownLocation() because until there is an update there will be no "last known location" You are missing that request provided by the LocationManager class:

public void requestLocationUpdates(java.lang.String provider, 
                                   long minTime,
                                   float minDistance, 
                                   android.location.LocationListener listener) 

If your activity implements LocationListener as mine did, you can pass in "this" as the LocationListener for a call originating in a method of that class. Otherwise, there are several other overloads taking Activity or Looper classes instead.

locationManager.requestLocationUpdates(provider, minTime, minDistance, this);

In order to make sure the device is getting a location, request updates - but you also may want to put guards for failure to find providers or getLastKnownLocation returning null.

locationManager.requestLocationUpdates(provider, 1, 0, this);
mostRecentLocation = locationManager.getLastKnownLocation(provider);
if(mostRecentLocation == null)
// lower your expectations, or send user message with Toast or something similar 

Tell the user to turn on their location sharing or other provider services.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
darKoram
  • 1,113
  • 1
  • 14
  • 25
0

The getLastKnownLocation method will only return a location if the gps LocationProvider has stored a location somewhere. If the user has not yet used the gps on his device long enough to obtain a valid GPS fix the method will return null.

Even if you retrieve a location check the time of the location, there is no guarantee that the location isn't very very old.

Janusz
  • 187,060
  • 113
  • 301
  • 369
  • thank you Janusz i solved it.eventually i gave the the code a few seconds of time to actually let the gps find the prevous location. – A.K Jun 01 '11 at 04:51