onLocationChanged() never called
LocationManager.NETWORK_PROVIDER
need network, but it is real, not precise; if you want to use LocationManager.GPS_PROVIDER
, the situation must be outdoor instead of indoor, because GPS location need satellite, if you are in any building, the satellite cannot find you!
I thing you forgot permission
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
OR
Declare Proper Class like
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location arg0) {
latitude = arg0.getLatitude();
longitude = arg0.getLongitude();
}
}
Do you never get a location or only as you write in your comment "sometimes it gets location but not at the time of click."?
Might be that your code is faster than LocationManager, which might not yet have called onLocationChanged(). Change your code in order to get the last known location, or wait until your locListener was called:
locManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 1000, 1, locListener);
mobileLocation = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (mobileLocation != null) {
i run my app on real device . i use network instead of GPS and onLocationChanged is called:
locMan.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, this, null);