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.
MainActivity
public class MainActivity extends Activity { GPSTracker mGPS = new GPSTracker(this); @Override protected void onCreate(Bundle savedInstanceState) { // ... if (mGPS.canGetLocation) { mGPS.getLocation(); // ... } else { // ... } } }
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; } }
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); } }
Manifest
Ensure the appropriate permissions are declared in the Manifest file:
Network-Based Location Only
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
GPS-Based Location Only
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
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!