1

My app is giving me the longitude and latitude of my location only when the net connection is there and when i switch it off it only shows me the last location without updating it.

public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                // First get location from Network Provider
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
arjun puri
  • 11
  • 2
  • Are you using Google for your API connection or are you using the built in Android sensors API? – mray190 Apr 25 '16 at 16:32
  • 1
    Possible duplicate of [Android: get current location of user without using gps or internet](http://stackoverflow.com/questions/6694391/android-get-current-location-of-user-without-using-gps-or-internet) – Roy Falk Apr 25 '16 at 16:47
  • i am only using the inbuilt sensors – arjun puri Apr 25 '16 at 16:53
  • You are using bad example code for getting the location. The problems of that infamous code are explained [in this blog post](http://gabesechansoftware.com/location-tracking/) which also has a suggestion for a better "GPS tracker" implementation. And no, [you don't need internet access to use GPS](https://stackoverflow.com/questions/12128990/does-gps-require-internet). Just implement location listening based on [Android documentation](https://developer.android.com/guide/topics/location/strategies.html) (not a bad idea) or some better example code. – Markus Kauppinen Apr 25 '16 at 17:31
  • 1
    @arjun puri; 2 of us have now linked to the same Location Strategies page, you should take that as an indication that you should study it. – Al Lelopath Apr 25 '16 at 17:46

1 Answers1

0

It's difficult to say since you don't post any code.
Does your manifest have these?

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

Location Strategies: Wi-Fi can ... provide a clue to users location

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139