How to get current exact location in android witho

2020-02-26 01:29发布

问题:

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;
}

回答1:

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.



回答2:

Without Internet and GPS , you can get the Location based on GSM ( Cell Network ).

The NETWORK_PROVIDER will resolve on the GSM or wifi, which ever available. Obviously with wifi off, GSM will be used. However,Cell network accuracy may vary from 500m to 2000M.