Analysis of three Android mobile phone location technologies
- 2020-05-07 20:25:38
- OfStack
// The statement LocationManager object
LocationManager loctionManager;
// Through the system service, get LocationManager object
loctionManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Method 1:
// through GPS The position provider gets the position
String providerGPS = LocationManager.GPS_PROVIDER;
Location location = loctionManager.getLastKnownLocation(providerGPS);
Method 2:
// The location is obtained through the base station location provider
String providerNetwork = LocationManager.NETWORK_PROVIDER;
Location location = loctionManager.getLastKnownLocation(providerNetwork);
Method 3:
// Use the standard set to let the system automatically select the best available location provider to provide the location
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);// High precision
criteria.setAltitudeRequired(false);// No altitude required
criteria.setBearingRequired(false);// No bearing requirement
criteria.setCostAllowed(true);// Allowable cost
criteria.setPowerRequirement(Criteria.POWER_LOW);// Low power consumption
// The best provider that matches the above criteria from the available location providers
String provider = loctionManager.getBestProvider(criteria, true);
// To obtain the final 1 The secondary position
Location location = loctionManager.getLastKnownLocation(provider);
Processing:
// Displayed in the EditText In the
updateWithNewLocation(location);
// Listen for changes in position, 2 seconds 1 Time, distance 10 Meters above
loctionManager.requestLocationUpdates(provider, 1000, 1, locationListener);
Monitor and display:
// Position monitor
private final LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
// Triggered when position changes
@Override
public void onLocationChanged(Location location) {
// Using the new location update TextView According to
updateWithNewLocation(location);
}
};
private void updateWithNewLocation(Location location) {
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latStr = format.format(lat);
lonStr = format.format(lng);
txtLat.setText(latStr);
txtLon.setText(lonStr);
} else {
txtLat.setText("");
txtLon.setText("");
}
}