J'ai un problème avec la lecture stable au début de cette application. Dans la première minute ou si la lecture devient très nerveux. Entre 10-100 mètres de différence, même en distance. J'ai entendu dire que c'est une erreur courante dans les applications de localisation en particulier celle utilisant le GPS. Puis-je empêcher cela d'une manière ou d'une autre avec une pause de lecture à ce démarrage? Avec un texte de fantaisie comme "Calculer votre emplacement" ou quelque chose comme ça pour les 10-20 premières secondes?
Et si oui. Où dois-je mettre en œuvre cette pause? Donc, il va obtenir des lectures et les stabiliser, mais ne pas gâcher les calculs? Ou y a-t-il un autre moyen d'éviter les erreurs de lecture? Ofc toute autre suggestion sur la mise à niveau du courant très bienvenue :)Location App sur Android
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
String addressString = "No address found";
String latLongString = "Your Current Position is: ";
double latitude = 10;
double longitude = 10;
double sLatitude;
double sLongitude;
double eLatitude;
double eLongitude;
double distance = 10;
double el1 = 0;
double el2 = 0;
double counter = 2;
TextView myLocationText;
TextView myLatitude;
TextView myLongitude;
TextView myAddress;
TextView startingLatitude;
TextView startingLongitude;
TextView endingLatitude;
TextView endingLongitude;
TextView myDistance;
TextView myLogOut;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
myLatitude = (TextView) findViewById(R.id.Latitude);
myLongitude = (TextView) findViewById(R.id.Longitude);
myLocationText = (TextView) findViewById(R.id.myLocationText);
myAddress = (TextView) findViewById(R.id.Address);
startingLatitude = (TextView) findViewById(R.id.startingLatitude);
startingLongitude = (TextView) findViewById(R.id.startingLongitude);
endingLatitude = (TextView) findViewById(R.id.endingLatitude);
endingLongitude = (TextView) findViewById(R.id.endingLongitude);
myDistance = (TextView) findViewById(R.id.distance);
myLogOut = (TextView) findViewById(R.id.logOut);
if (location != null) {
sLatitude = location.getLatitude();
sLongitude = location.getLongitude();
}
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 1000, 2,
locationListener);
}
private final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
private void updateWithNewLocation(Location location) {
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
eLatitude = location.getLatitude();
eLongitude = location.getLongitude();
distance = distance(sLatitude, eLatitude, sLongitude, eLongitude,
el1, el2);
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(latitude,
longitude, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
} catch (IOException e) {
}
} else {
latLongString = "No location found";
}
myLocationText.setText(latLongString);
myLatitude.setText("Latitude: " + latitude);
myLongitude.setText("Longitude: " + longitude);
myAddress.setText(addressString);
startingLatitude.setText("Starting latitude: " + sLatitude);
startingLongitude.setText("Starting longitude: " + sLongitude);
endingLatitude.setText("Last latitude: " + eLatitude);
endingLongitude.setText("Last longitude: " + eLongitude);
myDistance.setText("Your distance to starting point: " + distance);
}
public static double distance(double lat1, double lat2, double lon1,
double lon2, double el1, double el2) {
final int R = 6371; // Radius of the earth
double latDistance = Math.toRadians(lat2 - lat1);
double lonDistance = Math.toRadians(lon2 - lon1);
double a = Math.sin(latDistance/2) * Math.sin(latDistance/2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(lonDistance/2)
* Math.sin(lonDistance/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double distance = R * c * 1000; // convert to meters
double height = el1 - el2;
distance = Math.pow(distance, 2) + Math.pow(height, 2);
return Math.sqrt(distance);
}
}
Merci I était mince Malheureusement, nous devons utiliser geofence pour ce projet en quelque sorte la méthode des critères utiliser seulement GPS ce qu'il obtient en désordre Et il avait l'autorisation internet n manifeste – Tom
Je ne comprends pas ce que geofance a à voir avec vous ne pouvez pas utiliser googleApiClient, si quoi que ce soit, il sera plus facile à mettre en œuvre –
Vous pensez qu'il va résoudre le problème de saut? Il sera encore GPS Aussi en quelque sorte même avec un accès pour le réseau, je n'ai pas accès au service de localisation des réseaux dans les bâtiments avec une connexion Wi-Fi lorsque je suis connecté OFC. – Tom