2

I'm trying to make an application which gives current location to the user if GPS is On and shows an AlertDialog if not and asks for turning On the GPS.
But the method returns NULL only either GPS is On or Off... How can I solve it? I've looked for solution here and here but it didn't help.

Below is the getLocation() method that I'm using

    public Location getLocation() {
    try {

        // Getting GPS status
        isGPSEnabled = mLocationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // If GPS enabled, get latitude/longitude using GPS Services
        if (isGPSEnabled) {
            Toast.makeText(this,"using gps",Toast.LENGTH_LONG).show();
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, TIME, DISTANCE, this);
            if (mLocationManager != null) {
                mLocation = mLocationManager
                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (mLocation != null) {
                    mLatitude = mLocation.getLatitude();
                    mLongitude = mLocation.getLongitude();
                    isLocationAvailable = true; // setting a flag that
                    // location is available
                    return mLocation;
                }
            }
        }

        // If we are reaching this part, it means GPS was not able to fetch
        // any location
        // Getting network status
        isNetworkEnabled = mLocationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (isNetworkEnabled) {
            Toast.makeText(this,"using network",Toast.LENGTH_LONG).show();
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){

                    ActivityCompat.requestPermissions((Activity)getBaseContext(), new String[] {
                            android.Manifest.permission.ACCESS_FINE_LOCATION
                            },MY_PERMISSION_ACCESS_COURSE_LOCATION );

                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
               // return TODO;
            }
            mLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, TIME, DISTANCE, this);
                if (mLocationManager != null) {
                    mLocation = mLocationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (mLocation != null) {
                        mLatitude = mLocation.getLatitude();
                        mLongitude = mLocation.getLongitude();
                        isLocationAvailable = true; // setting a flag that
                        // location is available
                        return mLocation;
                    }
                }
            }
            // If reaching here means, we were not able to get location neither
            // from GPS not Network,
            if (!isGPSEnabled) {
                Toast.makeText(this,"turn on gps",Toast.LENGTH_LONG).show();
                // so asking user to open GPS
                askUserToOpenGPS();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        // if reaching here means, location was not available, so setting the
        // flag as false
        isLocationAvailable = false;
        return null;
    }

I have referred the above code from the answer to one of the above mentioned links.

Diksha
  • 406
  • 5
  • 20
  • 2
    Don't use that code. Its broken. It doesn't actually understand the difference between GPS being enabled, being on, and having a location lock. Its copied all over the net and its just wrong. A bteer solution and in depth explanation of how its wrong is found at http://gabesechansoftware.com/location-tracking/ – Gabe Sechan Aug 31 '17 at 19:30
  • oh thanks @GabeSechan for the information I'm now referring the link you gave – Diksha Aug 31 '17 at 19:37
  • In addition, just because GPS is enabled does not mean that the device is actively tracking the user's location. `getLastKnownLocation()` is an *optimization*, but you need to deal with the possibility that it returns `null`. – CommonsWare Aug 31 '17 at 19:37
  • Still getting `null` location @GabeSechan :( Shall I add this code to the `FallbackLocationTracker` class? `public Location getLocation() { locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); // checkPermission(locationManager); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,10, this); location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); return location; }` OR any other solution?? – Diksha Aug 31 '17 at 21:00
  • You can try using the hypertrack.com SDK for Android to get location reliably with a simple APIs – arjunattam Sep 03 '17 at 10:44

0 Answers0