How to get current exact location in android without internet connection?

19,030

The code you've show is just the getLocation method of your class. Presumably your class also implements LocationListener, correct? (Assuming it does without seeing it, because you assign "this" in requestLocationUpdates as the listener.)

The method you have shown will only ever return the LAST KNOWN location because location updates via providers are asynchronous. You indicate that you want to get location info via registering a listener, and that listener gets updates periodically (based on the settings you give it, and based on actual device capabilities and circumstances). You will NOT get the location back immediately after registering.

In the case of your question, getting location information while NOT connected to the network, you'll need to use the GPS provider (it works while not connected to the data network). Once you've registered a GPS provider listener (as you already have), you will get location updates via the callback in the listener, onLocationChanged(Location location).

What does your onLocationChanged callback do?

THAT is where you'll get updated location information a few seconds to minutes after you register the GPS provider (how soon depends on the hardware in the device you're using and on environmental conditions, GPS uses multiple satellites and works faster with a clear view of the sky, it may not work at all if you are indoors or the signal is otherwise obstructed).

In addition to waiting for the listener to get you updated location information you may also want to consider using some simpler, separate, methods to get the last known location and to register for the best provider. You can use a few simple loops and the Criteria you are interested in (COARSE vs FINE, LOW_POWER or not, etc).

The Android devs have documented this pretty well in the docs, and the "Protips for Location" open source sample app, and the "Deep Dive" blog post (which has links to all the other resources within it): http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html.

Share:
19,030
Admin
Author by

Admin

Updated on June 16, 2022

Comments

  • Admin
    Admin almost 2 years

    I want to get current exact location coordinates in android. I am able to get the location if mobile is connected to Wifi internet connection and but in other case when I go some where else and no internet connection available then it not giving the current location coordinates. instead giving last location coordinates.

    For example:

    Currently I am in A city and get current coordinates by on the wifi connection and move to City B then I am not able to fetch current coordinates. Instead of this its giving last location coordinates.

    Can anyone suggest me how can I get current location without internet connection.

    public Location getLocation() {
        try {
            locationManager = (LocationManager) context.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);
                    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);
                        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;
    }
    
    /**
     * Stop using GPS listener
     * Calling this function will stop using GPS in your app
     * */
    public void stopUsingGPS(){
        if(locationManager != null){
            locationManager.removeUpdates(GetLocations.this);
        }
    }
    
    /**
     * Function to get latitude
     * */
    public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }
        return latitude;
    }
    
    /**
     * Function to get longitude
     * */
    public double getLongitude(){
        if(location != null){
            longitude = location.getLongitude();
        }
        return longitude;
    }
    
  • Charlie Collins
    Charlie Collins about 11 years
    The poster already knows about the network provider, and is assigning it in the original question. The issue is that he doesn't wait for the listener. (Also, the network provider won't always use "GSM or Wifi," not all devices are GSM. And the network provider can be more accurate than min 500m, it certainly may not be, but that is not its best case.)