Home > Java > javaTutorial > body text

Why Am I Getting Outdated Location Coordinates Using Android\'s Network Provider?

Mary-Kate Olsen
Release: 2024-11-24 13:17:12
Original
494 people have browsed it

Why Am I Getting Outdated Location Coordinates Using Android's Network Provider?

How to Obtain Current Location in Android

Issue Description

Acquiring current location coordinates through the NETWORK provider of Android's location system proves challenging for the user, as they consistently receive outdated coordinates instead of current ones. Despite implementing multiple existing classes, the issue persists.

Code Snippets

MainActivity

public class MainActivity extends Activity {
    GPSTracker mGPS = new GPSTracker(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ...
        if (mGPS.canGetLocation) {
            mGPS.getLocation();
            // ...
        } else {
            // ...
        }
    }
}
Copy after login

GPSTracker

public class GPSTracker implements LocationListener {
    private LocationManager locationManager;

    public Location getLocation() {
        // ...
        if (isNetworkEnabled) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
                    MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
            // ...
        }
        // ...
        return location;
    }
}
Copy after login

Proposed Solution

The issue can be attributed to insufficient location updates. Here's a modified code snippet to resolve it:

MainActivity

public class MainActivity extends Activity {
    private LocationListener mLocationListener = new LocationListener() {
        // ...
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ...
        LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
                LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, mLocationListener);
    }
}
Copy after login

Manifest

Ensure the appropriate permissions are declared in the Manifest file:

Network-Based Location Only

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Copy after login

GPS-Based Location Only

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Copy after login

The above is the detailed content of Why Am I Getting Outdated Location Coordinates Using Android\'s Network Provider?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template