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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| public class Gps extends Activity {
double latEnd;
double lonEnd;
double latStart;
double lonStart;
double distance;
TextView tPosition;
TextView tDestination;
TextView tDistance;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
//On récupère lIntent que lon a reçu
Intent thisIntent = getIntent();
//On récupère les deux extra grâce à leurs id
latEnd = Math.toRadians(thisIntent.getExtras().getDouble("latEnd"));
lonEnd = Math.toRadians(thisIntent.getExtras().getDouble("lonEnd"));
//Positionne les textes
tPosition = (TextView) findViewById(R.id.textPosition);
tDestination = (TextView) findViewById(R.id.textDestination);
tDistance = (TextView) findViewById(R.id.textDistance);
RefreshText();
//On récupère le service de localisation
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager .requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, mlocListener);
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location){
double R = 6371; // Rayon de la terre
latStart = Math.toRadians(location.getLatitude());
lonStart = Math.toRadians(location.getLongitude());
distance = Math.acos(Math.sin(latStart)*Math.sin(latEnd) + Math.cos(latStart)*Math.cos(latEnd) * Math.cos(lonEnd-lonStart)) * R;
RefreshText();
}
public void onProviderDisabled(String provider){
Toast.makeText(getApplicationContext(),"Gps Désactivé",Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider){
Toast.makeText( getApplicationContext(),"Gps Activé",Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras){
}
}
private void RefreshText(){
tPosition.setText("Position actuelle = " + latStart + ", " + lonStart);
tDestination.setText("Destination actuelle = " + latEnd + ", " + lonEnd);
tDistance.setText("Distance réstante = " + distance);
}
} |
Partager