1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
private void obtenirPosition() {
setProgressBarIndeterminateVisibility(true);
// On récupère le service de localisation
lManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
//Lorsque la position change...
Log.i("Géolocalisation", "La position a changé.");
//on stop le cercle de chargement
setProgressBarIndeterminateVisibility(false);
//on sauvegarde la position
//... on l'affiche
afficherLocation(location);
//... et on spécifie au service que l'on ne souhaite plus avoir de mise à jour
lManager.removeUpdates(this);
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i("Géolocalisation", "Le statut de la source a changé.");
}
public void onProviderEnabled(String provider) {
Log.i("Géolocalisation", "La source a été activé.");
}
public void onProviderDisabled(String provider) {
//Lorsque le GSP est désactivé
Log.i("Géolocalisation", "La source a été désactivé");
//...on affiche un Toast pour le signaler à l'utilisateur
Toast.makeText(okgeolocalise.this,
String.format("Le GPS a été désactivé", provider),
Toast.LENGTH_SHORT).show();
//on spécifie au service que l'on ne souhaite plus avoir de mise à jour
lManager.removeUpdates(this);
//on stop le cercle de chargement
setProgressBarIndeterminateVisibility(false);
}
};
lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, locationListener);
}
private void afficherLocation(Location location) {
//On affiche les informations de la position a l'écran
((TextView)findViewById(R.id.latitude)).setText(String.valueOf(location.getLatitude()));
((TextView)findViewById(R.id.longitude)).setText(String.valueOf(location.getLongitude()));
} |
Partager