I am trying to fetch location based on WiFi/3G/4G
connection but it always returns 0.0
as latitude and longitude
. If same code is used
When you call locationManager.requestLocationUpdates()
this tells Android that you would like to be called back when the user's location changes. If you don't have GPS enabled and you want to use NETWORK_PROVIDER, you need to have Internet access. This all happens asynchronously. You call requestLocationUpdates()
and then some time later, Android will call you back by calling onLocationChanged()
with the new location.
What you are doing after calling requestLocationUpdates()
is immediately calling getLastKnownLocation()
which may or may not return something useful. In your implementation of onLocationChanged()
you are doing nothing. You will need to wait until Android calls onLocationChanged()
at which point you should store the passed Location
parameter in your member variable.