0

I have a method which registers a listener and requests location updates. I would like to the location coordinates in the current method itself, not in the onLocationChanged method.

My code is:-

myclass {

   onCreate {
      location_Manager = (LocationManager) getSystemService (Context.LOCATION_SERVICE);
      locLstnr = new myLocationlistener();
      locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, Loc_listener.MINIMUM_TIME_BETWEEN_UPDATES, Loc_listener.MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, locLstnr);
   }

   class myLocationListener () {
      onLocationChanged() { ... }
      onProviderDisabled() { ... }
      onProviderEnabled() { ... }
   }

}

I know i can get getLastKnownLocation() , but incase it is not available, isn't it possible to get the location at all? This and this are a similar questions, but doesn't help me. I don't have problems with using onLocationChanged method, but i need to have the location in the onCreate method. Is that possible? One way might be to store the location onto a variable in the myLocationListener and access it, but is there an alternative or a better solution?

Community
  • 1
  • 1
gkris
  • 1,209
  • 3
  • 17
  • 44

1 Answers1

1

I think if getLastKnownLocation() gives null then you have to wait for call back....

just can do can make request for update

locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener); 

http://devdiscoveries.wordpress.com/2010/02/04/android-use-location-services/

from link

getLastKnownLocation() is faster after a connection has been established with the GPS satellite. For the first time, it will return null, or no value till no connection is established. You can add a GpsListener to know when the location is obtained. Search about "how to get a gps fix" and you might get answer to your question

and refer link

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36