coder
coder

Reputation: 10520

Can't get location from Android

I'm trying to get the location of my device, but have only been successful one time out of running the code several times. Below is the code I'm using:

    final LocationListener mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.d(TAG, location.getLatitude() + ", " + location.getLongitude());
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {
            Log.d(TAG, "changed: " + s);
        }

        @Override
        public void onProviderEnabled(String s) {
            Log.d(TAG, "enabled: " + s);
        }

        @Override
        public void onProviderDisabled(String s) {
            Log.d(TAG, "disabled: " + s);
        }
    };

    LocationManager mLocationManager = (LocationManager) getActivity().getSystemService(getActivity().getApplicationContext().LOCATION_SERVICE);

    boolean enabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    Log.d(TAG, "gps enabled?" + enabled);
    Log.d(TAG, "network enabled?" +  mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));

    Criteria criteria = new Criteria();
    best = mLocationManager.getBestProvider(criteria, true);

    Log.d(TAG, best);
    Log.d(TAG, LocationManager.GPS_PROVIDER);
    Log.d(TAG, LocationManager.NETWORK_PROVIDER);

    mLocationManager.requestLocationUpdates(best, 0, 0, mLocationListener);

I am indoors (I would like this to work indoors), so I keep getting the best to be network. Both the gps enabled and network enabled strings are printing out true. Is the reason that I am inconsistently getting a location, or is there something I can do to fix this so I can get a location indoors?

Upvotes: 0

Views: 108

Answers (1)

shemsu
shemsu

Reputation: 1076

So for device location, using the Fused provider allows to get far better results.

In addition, you will not need to manage different providers.

Android developer documentation about location

Upvotes: 2

Related Questions