4

I'm trying to get my location using it like this:

LocationManager myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Criteria criteria = new Criteria(); 
criteria.setAccuracy(Criteria.ACCURACY_FINE); 
criteria.setAltitudeRequired(false); 
criteria.setBearingRequired(false); 
criteria.setCostAllowed(true); 
criteria.setPowerRequirement(Criteria.POWER_LOW); 

String provider = myLocationManager.getBestProvider(criteria,true); 

if (provider != null) { 
    //there is a provider
    myLocationManager.requestLocationUpdates(provider, 10L, 500.0f, (LocationListener) mainContext);
    Location myCurLocation = myLocationManager.getLastKnownLocation(provider); 
            //here i'm trying to get some data from the
            //myCurLocation but 
            //myCurLocation == NULL
}

but the myCurLocation is always == NULL

What am I doing wrong?

Janusz
  • 187,060
  • 113
  • 301
  • 369
gmadar
  • 1,884
  • 3
  • 19
  • 22

1 Answers1

8

The call to getLastKnownLocation() doesn't block - which means it will return null if no position is currently available - so you probably want to have a look at passing a LocationListener to the requestLocationUpdates() method instead, which will give you asynchronous updates of your location.

Have a look at this question for an example of using a LocationListener.

Community
  • 1
  • 1
David Webb
  • 190,537
  • 57
  • 313
  • 299