0

I am trying to fetch the logged in user's lat and long using LocationManager. I am able to get the Permissions Screen and after allowing i want to see the Lat and Long on the Text View. But, i am not able to see anything in the text view.

protected void getLocation() {
        Log.v(TAG, "GetLocation");
        int LOCATION_REFRESH_TIME = 1000;
        int LOCATION_REFRESH_DISTANCE = 5;

        if (!(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
            Log.v("WEAVER_", "Has permission");
            mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME,
                    LOCATION_REFRESH_DISTANCE, mLocationListener);

        } else {
            Log.v("WEAVER_", "Does not have permission");
        }

    }

Now, i am calling mLocationListener, but not getting any results neither any error. OnLocationChanged() method is not getting called.

 private final LocationListener mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.v("WEAVER_", "Location Change");
            double latitude = location.getLatitude();
            textView.setText(String.valueOf(latitude) + "latitude");
            textView.setText(String.valueOf(updates) + " updates");
        }

Here, the textView does not show anything, neither the log is updated with Location Changed Event.

The Log results are:

    08-21 14:35:29.336 3390-3390/daveytree.geo2 V/WEAVER_: GetLocation
08-21 14:35:29.337 3390-3390/daveytree.geo2 V/WEAVER_: Has permission
08-21 14:35:29.362 3390-3526/daveytree.geo2 D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

Is there any other event to capture this. Please help

Sourav Das
  • 982
  • 1
  • 15
  • 40
  • check if your device is in airplane mode and/or position detector is disable in the device. – Vyacheslav Aug 21 '16 at 18:51
  • LocationManager is part of old Android Location Framework, Google strongly recommends moving to Google Play Location Services (I'm using them in my app and everything works fine). You can read more about it here: https://developer.android.com/training/location/index.html – Krzysiek Aug 21 '16 at 19:21

1 Answers1

2

From this SO question, you can try to change the GPS_PROVIDER to NETWORK_PROVIDER. Because GPS_PROVIDER determines the location using satellites, depending on conditions, this provider may take a while to return a location fix.

For more information, you can also check this related SO questions:

Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31