user3459994 user3459994 - 8 days ago 5
Android Question

Android: requestLocationUpdates

I have managed to get a fix on an android device's location (both with network provider and gps provider) using:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);


but i would like calculate the phones location at the same moment once using the NETWORK_PROVIDER and then the GPS_PROVIDER so that i can compare each accuracy together.

Does anyone know how to pinpoint the device once with NETWORK_PROVIDER and then with GPS_PROVIDER?

s23 s23
Answer

Use 2 Location Listeners

public class MainActivity extends Activity {
    private Location networkLocation = null;
    private Location gpsLocation = null;

    private class NetworkLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            // if you only want one location
            // if (networkLocation == null)
            networkLocation = location;
            if (gpsLocation != null) {
                // do something
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}

        @Override
        public void onProviderEnabled(String provider) {}

        @Override
        public void onProviderDisabled(String provider) {}
    }

    private class GpsLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            // if you only want one location
            // if (gpsLocation == null)
            gpsLocation = location;
            if (networkLocation != null) {
                // do something
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}

        @Override
        public void onProviderEnabled(String provider) {}

        @Override
        public void onProviderDisabled(String provider) {}
    }

}