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 69 70 71 72 73 74
|
locationListener = new MyLocationListener();
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //control location updates
//lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 2, locationListener);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 2, locationListener);
/* On va choper le best providers */
//Criteria crit = new Criteria();
//towers = lm.getBestProvider(crit, false);
//Location location = lm.getLastKnownLocation(towers);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE); //Or whatever criteria you want
towers = lm.getBestProvider(crit, true);
Location location = lm.getLastKnownLocation(towers);
if (location != null) {
lat = location.getLatitude();
longi = location.getLongitude();
String latit = String.valueOf(lat);
String longit = String.valueOf(longi);
tvLat = (TextView) view.findViewById(R.id.tvLatitude);
tvLongi = (TextView) view.findViewById(R.id.tvLongitude);
tvAdr = (TextView) view.findViewById(R.id.tvAdresse);
tvLat.setText(latit);
tvLongi.setText(longit);
//Le geocoder permet de récupérer ou chercher des adresses
//gràce à un mot clé ou une position
// to access the adress
Geocoder geocoder = new Geocoder(NoteReminder.this, Locale.getDefault());
try{
//max result = 1 because we only want one result
List<Address> address = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (address.size() > 0){
String display ="";
for(int i = 0; i<address.get(0).getMaxAddressLineIndex(); i++){
display += address.get(0).getAddressLine(i) + "\n";
}
tvAdr.setText(display);
}
else{
tvAdr.setText("L'adresse n'a pu être déterminée");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
String error = e.toString();
tvAdr.setText(error);
}
}
else{
Toast.makeText(getBaseContext(),
"Localisation actuelle FOIRE ",
Toast.LENGTH_SHORT).show();
} |
Partager